Java Programming — Basics & Syntax
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in Basics & Syntax
Q.21 Medium Basics & Syntax
Which of the following correctly represents variable declaration and initialization in Java?
A int x; x = 5.5;
B double y = 10;
C boolean flag = true;
D String 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).

Take Test
Q.22 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.

Take Test
Q.23 Medium Basics & Syntax
Which statement about constructors is INCORRECT?
A Constructors can be overloaded
B Constructors must return a value
C Constructors are called automatically when an object is created
D A 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.

Take Test
Q.24 Medium Basics & Syntax
What is the difference between 'break' and 'continue' in loop statements?
A Both terminate the loop
B break exits the loop; continue skips current iteration
C continue exits the loop; break skips current iteration
D They have the same functionality
Correct Answer:  B. break exits the loop; continue skips current iteration
EXPLANATION

'break' immediately exits the loop, while 'continue' skips the remaining statements in the current iteration and jumps to the next iteration.

Take Test
Q.25 Easy Basics & Syntax
Which of the following is true about the 'break' statement?
A It terminates the entire program
B It skips the current iteration of a loop
C It exits the current loop or switch statement
D It 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.

Take Test
Advertisement
Q.26 Easy Basics & Syntax
What is the output of: System.out.println(10 / 3);?
A 3.333...
B 3.33
C 3
D 4
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.

Take Test
Q.27 Medium Basics & Syntax
Which access modifier allows a member to be accessible only within the same package?
A public
B private
C protected
D default (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.

Take Test
Q.28 Medium Basics & Syntax
What will happen if you try to access an index out of bounds in an array?
A Returns 0
B Returns null
C Throws ArrayIndexOutOfBoundsException
D Compilation 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.

Take Test
Q.29 Medium Basics & Syntax
Which of the following statements about static methods is correct?
A Static methods can access instance variables directly
B Static methods can be overridden by subclasses
C Static methods can only be called using the class name
D Static 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).

Take Test
Q.30 Easy Basics & Syntax
What is the size of 'long' data type in Java?
A 16 bits
B 32 bits
C 64 bits
D Platform dependent
Correct Answer:  C. 64 bits
EXPLANATION

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.

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