Home Subjects Java Programming Basics & Syntax

Java Programming
Basics & Syntax

Java OOP, collections, multithreading

16 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–16 of 16
Topics in Java Programming
Q.11 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.12 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.13 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.14 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
Q.15 Hard Basics & Syntax
What will be printed? System.out.println(10 + 20 + "Java");
A 30Java
B 10 + 20 + Java
C 1020Java
D Compilation error
Correct Answer:  A. 30Java
EXPLANATION

String concatenation works left to right. 10 + 20 = 30 (integer addition), then 30 + "Java" = "30Java" (string concatenation).

Test
Q.16 Hard Basics & Syntax
Consider the code: int a = 5; int b = a++; System.out.println(a + " " + b); What is the output?
A 5 5
B 6 5
C 5 6
D 6 6
Correct Answer:  B. 6 5
EXPLANATION

The post-increment operator (a++) returns the value before incrementing. So b = 5, then a becomes 6. Output: 6 5

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