Which of the following correctly represents variable declaration and initialization in Java?
Aint x; x = 5.5;
Bdouble y = 10;
Cboolean flag = true;
DString name = 123;
Correct Answer:
C. boolean flag = true;
EXPLANATION
Option C is correct. Option A causes a compilation error (cannot assign double to int). Option B works but there's implicit conversion. Option D causes a compilation error (cannot assign int to String).
What will be the output of: int x = 5; System.out.println(++x + x++);?
A11
B12
C13
D10
Correct Answer:
B. 12
EXPLANATION
++x increments x to 6 and returns 6 (pre-increment). Then x++ returns 6 and increments x to 7 (post-increment). So 6 + 6 = 12. The order of evaluation in expressions like this depends on operator precedence and associativity.
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.