CConstructors are called automatically when an object is created
DA class can have multiple constructors
Correct Answer:
B. Constructors must return a value
EXPLANATION
Constructors do NOT have a return type, not even void. They automatically initialize objects when created. They can be overloaded and a class can have multiple constructors with different parameters.
Which of the following is true about the 'break' statement?
AIt terminates the entire program
BIt skips the current iteration of a loop
CIt exits the current loop or switch statement
DIt jumps to the next iteration
Correct Answer:
C. It exits the current loop or switch statement
EXPLANATION
'break' statement exits the innermost loop or switch statement. It doesn't skip the current iteration (that's 'continue') and doesn't terminate the entire program.
What is the output of: System.out.println(10 / 3);?
A3.333...
B3.33
C3
D4
Correct Answer:
C. 3
EXPLANATION
When dividing two integers, Java performs integer division and returns an integer result. 10 / 3 = 3 (remainder is discarded). To get decimal result, at least one operand should be a float or double.
Which access modifier allows a member to be accessible only within the same package?
Apublic
Bprivate
Cprotected
Ddefault (no modifier)
Correct Answer:
D. default (no modifier)
EXPLANATION
When no access modifier is specified, it's called package-private or default access. Members with default access are accessible only within the same package, not from other packages.
What will happen if you try to access an index out of bounds in an array?
AReturns 0
BReturns null
CThrows ArrayIndexOutOfBoundsException
DCompilation error
Correct Answer:
C. Throws ArrayIndexOutOfBoundsException
EXPLANATION
Accessing an array index that doesn't exist throws an ArrayIndexOutOfBoundsException at runtime. This is a checked exception that must be handled or declared.
Which of the following statements about static methods is correct?
AStatic methods can access instance variables directly
BStatic methods can be overridden by subclasses
CStatic methods can only be called using the class name
DStatic methods must have a return type
Correct Answer:
C. Static methods can only be called using the class name
EXPLANATION
Static methods belong to the class, not to any instance. They are called using the class name (e.g., ClassName.methodName()). They cannot directly access instance variables and cannot be overridden (though they can be hidden).
In Java, the 'long' data type is always 64 bits, regardless of the platform. This is one of Java's strengths - its size specifications are platform-independent.
Which of the following correctly demonstrates method overloading?
ATwo methods with same name and same parameters in different classes
BTwo methods with same name but different number or type of parameters in the same class
CTwo methods with different names and different parameters in the same class
DTwo methods with same name and return type in the same class
Correct Answer:
B. Two methods with same name but different number or type of parameters in the same class
EXPLANATION
Method overloading allows multiple methods with the same name but different parameters (number, type, or order) in the same class. This is a way to implement compile-time (static) polymorphism.
Consider the following code: int x = 5; x += 3; System.out.println(x); What is the output?
A5
B8
C53
DCompilation error
Correct Answer:
B. 8
EXPLANATION
The += operator is an assignment operator that adds the right operand to the left operand and assigns the result. So x += 3 means x = x + 3, which is 5 + 3 = 8.