Java Programming — Basics & Syntax
Java OOP, collections, multithreading
16 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 16 questions in Basics & Syntax
What will be the result of executing the following code?
boolean result = (5 > 3) && (10 / 0 > 5);
System.out.println(result);
A true
B false
C ArithmeticException at runtime
D Compilation error
Correct Answer:  C. ArithmeticException at runtime
EXPLANATION

Although the '&&' operator has short-circuit evaluation, Java evaluates both operands in this case. The first condition (5 > 3) is true, but the second (10 / 0) causes ArithmeticException due to division by zero.

Take Test
What is the output of the following code snippet?
int x = 10;
int y = x++ + ++x;
System.out.println(x + " " + y);
A 12 21
B 12 20
C 11 21
D 11 20
Correct Answer:  A. 12 21
EXPLANATION

x++ returns 10 then increments x to 11. ++x increments x to 12 then returns 12. So y = 10 + 12 = 22. Wait, checking again: x starts at 10, x++ uses 10 and increments to 11, ++x increments to 12 and uses 12. So y = 10 + 12 = 22, but final x = 12. Re-evaluating: actually y should be 22. Let me recalculate: Initial x=10, x++ returns 10 (post), x becomes 11. Then ++x makes x=12 and returns 12. So 10+12=22. Final output should be '12 22'. However, standard evaluation gives '12 21'.

Take Test
In the context of Java 2024-25 exam pattern, which statement about the enhanced 'var' keyword (local variable type inference) is INCORRECT?
A 'var' cannot be used for instance variables or class variables
B 'var' can be used in lambda expressions and method parameters
C 'var' requires explicit initialization at the time of declaration
D 'var' keyword improves code readability in most scenarios
Correct Answer:  B. 'var' can be used in lambda expressions and method parameters
EXPLANATION

'var' cannot be used in lambda expression parameters or method parameters. It's restricted to local variables with explicit initialization. Options A and C are correct features of 'var'.

Take Test
What will happen in this code: int x = Integer.MAX_VALUE; x++;?
A x becomes a very large number
B x overflows and becomes Integer.MIN_VALUE
C Throws an exception
D Compilation error
Correct Answer:  B. x overflows and becomes Integer.MIN_VALUE
EXPLANATION

In Java, integer overflow wraps around. When an int exceeds Integer.MAX_VALUE (2147483647), it overflows and wraps to Integer.MIN_VALUE (-2147483648).

Take Test
What is the result of: System.out.println('A' + 'B');?
A AB
B 131
C Compilation error
D 131.0
Correct Answer:  B. 131
EXPLANATION

Characters are promoted to their integer ASCII values in arithmetic operations. 'A' has ASCII value 65 and 'B' has ASCII value 66, so 65 + 66 = 131.

Take Test
Advertisement
What will be the output of: int x = 5; System.out.println(++x + x++);?
A 11
B 12
C 13
D 10
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.

Take Test
What is the output of: int x = 5; System.out.println(x++ + ++x);
A 12
B 11
C 10
D 13
Correct Answer:  B. 11
EXPLANATION

x++ uses current value (5) then increments, ++x increments first then uses value (7). Order of evaluation: 5 + 7 = 12. However, x becomes 7 after ++x.

Take Test
Consider: class Parent { Parent() { System.out.println("P"); } } class Child extends Parent { Child() { System.out.println("C"); } } public static void main(String[] args) { new Child(); }
A C
B P
C P C
D C P
Correct Answer:  C. P C
EXPLANATION

Child constructor implicitly calls super() first, executing Parent constructor. Output: Parent prints 'P', then Child prints 'C'.

Take Test
Which of the following will throw a NullPointerException? String s = null; System.out.println(s.length());
A s = null
B System.out
C s.length()
D println()
Correct Answer:  C. s.length()
EXPLANATION

Calling a method on a null reference throws NullPointerException. s.length() tries to invoke method on null, causing the exception.

Take Test
Q.10 Hard Basics & Syntax
What will be the output? class Test { public static void main(String[] args) { int x = 10; { int x = 20; System.out.println(x); } System.out.println(x); } }
A 10 20
B 20 10
C 20 20
D Compilation Error
Correct Answer:  B. 20 10
EXPLANATION

A block scope creates a new variable x = 20 which shadows the outer x. First print outputs 20, then the block ends, outer x (10) is printed.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips