NEW
Font size
WorksheetsJava Programming Basics Assessment
Total questions: 50
Worksheet time: 40mins
What is the correct way to declare a variable in Java?
int variableName;
int: variableName;
var variableName int;
int variableName = 0;
Which of the following is a valid data type in Java?
int
boolean
string
float
What is the output of the following code: System.out.println(5 + 3);?
8
10
9
6
Which keyword is used to create a class in Java?
define
class
create
function
What is the default value of a boolean variable in Java?
1
null
false
true
Which control structure is used to execute a block of code multiple times?
Function
Condition
Loop
Switch
What is the purpose of the 'break' statement in Java?
The 'break' statement is used to skip the current iteration of a loop.
The 'break' statement is used to define a new method in Java.
The 'break' statement is used to pause execution of a program.
The 'break' statement is used to exit loops or switch statements.
Which of the following is NOT a valid loop in Java?
repeat-until
foreach
while
do-while
What is the output of the following code: System.out.println(10 > 5 ? 'A' : 'B');?
C
A
B
10
What is the main principle of Object-Oriented Programming?
Data encapsulation only
Static typing only
Encapsulation, inheritance, polymorphism, and abstraction.
Single inheritance only
Which of the following is a feature of Object-Oriented Programming?
Encapsulation
Inheritance
Abstraction
Polymorphism
What keyword is used to inherit a class in Java?
implements
inherits
derives
extends
What is the purpose of the 'this' keyword in Java?
The 'this' keyword is used to refer to the current object instance in Java.
The 'this' keyword is used to access global variables in Java.
The 'this' keyword is used to create new classes in Java.
The 'this' keyword is used to define static methods in Java.
Which of the following is an example of method overloading?
Overriding a method in a subclass with the same signature.
Defining multiple methods with the same name but different parameter lists.
Creating a single method with multiple return types.
Defining methods with different names but the same parameters.
What is encapsulation in Object-Oriented Programming?
Encapsulation is the process of creating multiple instances of a class.
Encapsulation refers to the inheritance of properties from a parent class.
Encapsulation is the method of exposing all data and methods of an object to the outside world.
Encapsulation is the bundling of data and methods that operate on that data, restricting access to some of the object's components.
What is the output of the following code: int x = 10; if (x < 5) { System.out.println('Low'); } else { System.out.println('High'); }?
Low
Very High
High
Medium
Which of the following statements is true about arrays in Java?
Arrays in Java are fixed in size and can hold elements of the same type.
Arrays in Java are always initialized with default values.
Arrays in Java can hold elements of different types.
Arrays in Java can grow dynamically in size.
What is the correct syntax for a for loop in Java?
for(initialization, condition, increment) { // loop body }
for(initialization; increment; condition) { // loop body }
for(initialization; condition; increment) { // loop body }
for(condition; initialization; increment) { // loop body }
What is polymorphism in Object-Oriented Programming?
Polymorphism is the ability to create multiple instances of a class.
Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface.
Polymorphism is the ability of a class to inherit properties from multiple classes.
Polymorphism refers to the process of combining multiple classes into one.
Which of the following is a valid way to create an object in Java?
MyClass obj = create MyClass();
MyClass obj = new MyClass();
MyClass obj = MyClass();
MyClass obj : new MyClass();
What is the purpose of the 'super' keyword in Java?
The 'super' keyword is used to call a method from the parent class.
The 'super' keyword is used to access private members of a class.
The 'super' keyword is used to create a new instance of a class.
The 'super' keyword is used to define a static variable.
Which of the following is a characteristic of an abstract class in Java?
An abstract class cannot be instantiated.
All methods in an abstract class must be abstract.
An abstract class must have at least one abstract method.
An abstract class can have constructors.
What is the output of the following code: String str = "Hello"; System.out.println(str.length());?
5
4
6
5.0
Which of the following is a characteristic of a constructor in Java?
A constructor can have a return type.
A constructor is called when an object is created.
A constructor must be private.
A constructor can be inherited.
What is the output of the following code: System.out.println("Hello".toUpperCase());?
Hello
HELLO!
hello
HELLO
Which of the following keywords is used to define an interface in Java?
interface
implements
class
abstract
What is the output of the following code: int[] arr = {1, 2, 3}; System.out.println(arr[1]);?
3
Array Index Out of Bounds
2
1
Which of the following is a valid way to declare a method in Java?
myMethod void() {}
void: myMethod() {}
myMethod() void {}
void myMethod() {}
What is the purpose of the 'final' keyword in Java?
The 'final' keyword is used to prevent method overriding.
The 'final' keyword is used to declare constants.
All of the above.
The 'final' keyword is used to prevent inheritance.
What is the purpose of the 'static' keyword in Java?
To indicate that a method is abstract.
To create a constant variable.
To declare a variable that can be modified.
To define a method that can be called without creating an instance of the class.
Which of the following is true about method overriding in Java?
It is not allowed in Java.
It can only be done with static methods.
It requires the method in the subclass to have a different name.
It allows a subclass to provide a specific implementation of a method already defined in its superclass.
What is the output of the following code: String str = "Java"; System.out.println(str.charAt(2));?
J
v
a
Index Out of Bounds
What is the output of the following code: String str = "Java"; System.out.println(str.charAt(2));?
v
J
Java
a
Which of the following statements is true about interfaces in Java?
Interfaces can have constructors.
Interfaces can extend multiple other interfaces.
Interfaces can have method implementations.
Interfaces can be instantiated directly.
What is the purpose of the 'this' keyword in Java?
To call a static method.
To refer to the current object.
To refer to the parent class.
To create a new instance of a class.
What is the output of the following code: String str = "Hello World"; System.out.println(str.substring(6, 11));?
World!
o W
World
Hello
Which of the following is a valid way to create an array in Java?
int arr = new int[5];
int arr = new int[];
int arr[] = int[5];
int[] arr = new int[5];
What is the output of the following code: System.out.println(10 == 10 ? "Equal" : "Not Equal");?
True
10
Not Equal
Equal
What is the purpose of the 'super' keyword in Java?
To call a method from the parent class.
To create a new instance of a class.
To define a constant variable.
To refer to the current class.
Which of the following is a feature of Java's exception handling?
Exceptions must be caught or declared to be thrown.
All exceptions are checked exceptions.
Exceptions can only be handled in the main method.
Java does not support exception handling.
What is the output of the following code: String str = "Hello"; System.out.println(str.length());?
5
4
6
Null
What is the output of the following code: System.out.println(3 * 4 + 2);?
8
10
12
14
Which of the following is a valid way to declare a method in Java?
void myMethod() {}
myMethod() void {}
void: myMethod() {}
myMethod void() {}
What is the output of the following code: String str = "Hello"; System.out.println(str.length());?
5.0
6
4
5
What is the output of the following code: int x = 5; System.out.println(x++);?
6
Compilation Error
Runtime Error
5
Which of the following is a valid way to implement an interface in Java?
class MyClass : MyInterface {}
class MyClass implements MyInterface()
class MyClass extends MyInterface {}
class MyClass implements MyInterface {}
What is the purpose of the 'this' keyword in Java?
To refer to the parent class.
To call a static method.
To create a new instance of a class.
To refer to the current object instance.
What is the output of the following code: int a = 10; int b = 20; System.out.println(a + b);?
20
10
30
1020
Which of the following is a valid way to declare a constructor in Java?
void MyClass() {}
MyClass() {}
MyClass() : {}
MyClass {}
What is the purpose of the 'final' keyword in Java?
To declare a constant variable.
To prevent method overriding.
To prevent inheritance of a class.
All of the above.
