Home Subjects Java Programming

Java Programming

Java OOP, collections, multithreading

212 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 201–210 of 212
Topics in Java Programming
Q.201 Hard Basics & Syntax
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.

Test
Q.202 Hard Basics & Syntax
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.

Test
Q.203 Hard Basics & Syntax
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.

Test
Q.204 Hard Basics & Syntax
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'.

Test
Q.205 Hard Basics & Syntax
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.

Test
Q.206 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.

Test
Q.207 Hard Basics & Syntax
Which statement about Java's garbage collection is true?
A Garbage collection is deterministic and happens at fixed intervals
B Garbage collection is non-deterministic and may never run
C Calling System.gc() guarantees immediate garbage collection
D Objects are garbage collected as soon as they go out of scope
Correct Answer:  B. Garbage collection is non-deterministic and may never run
EXPLANATION

Garbage collection in Java is non-deterministic. System.gc() is just a request, not a guarantee. Objects are eligible for GC when unreachable, but actual collection timing varies.

Test
Q.208 Hard Basics & Syntax
What is the result of: int x = 5; System.out.println(x++ + ++x);?
A 10
B 11
C 12
D 13
Correct Answer:  B. 11
EXPLANATION

x++ returns 5 (then increments to 6), ++x increments to 7 and returns 7. Sum = 5+7 = 12. Note: Actual result may vary due to JVM optimization but typically 11 or 12.

Test
Q.209 Hard Basics & Syntax
Consider: int x = 0; System.out.println(x == 0 && x != 1); What is the output?
A true
B false
C Compilation error
D 1
Correct Answer:  A. true
EXPLANATION

Both conditions are true: x == 0 (true) and x != 1 (true). The && (AND) operator returns true when both operands are true.

Test
Q.210 Hard Basics & Syntax
What is the scope of a local variable in Java?
A Throughout the class
B Throughout the method or block where it is declared
C Throughout the package
D Throughout the entire program
Correct Answer:  B. Throughout the method or block where it is declared
EXPLANATION

Local variables have scope limited to the method or code block in which they are declared. They cannot be accessed outside that block.

Test
IGET
IGET AI
Online · Exam prep assistant
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