Home Subjects Java Programming Basics & Syntax

Java Programming
Basics & Syntax

Java OOP, collections, multithreading

50 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 21–30 of 50
Topics in Java Programming
Q.21 Medium Basics & Syntax
What is the output of: System.out.println(true && false || true);
A true
B false
C null
D Compilation Error
Correct Answer:  A. true
EXPLANATION

Evaluation: (true && false) || true = false || true = true. AND operator (&& has higher precedence than OR (||).

Test
Q.22 Medium Basics & Syntax
Which of the following will result in a compilation error? class Test { public void method1() { } public void method1(int x) { } }
A Method overloading
B Method overriding
C No error, valid code
D Syntax error
Correct Answer:  C. No error, valid code
EXPLANATION

This is valid method overloading. Two methods with same name but different parameters (method signature) is allowed in Java.

Test
Q.23 Medium Basics & Syntax
What is the scope of a variable declared inside a method in Java?
A Global
B Local
C Static
D Package-level
Correct Answer:  B. Local
EXPLANATION

Variables declared inside a method have local scope and are only accessible within that method. They are destroyed when the method returns.

Test
Q.24 Medium Basics & Syntax
Consider: class A { static int x = 5; } How many times is x initialized when you create multiple A objects?
A Once per object creation
B Once when the class is loaded
C Multiple times depending on object count
D Twice (once for class, once for object)
Correct Answer:  B. Once when the class is loaded
EXPLANATION

Static variables are initialized only once when the class is first loaded into memory, regardless of object creation.

Test
Q.25 Medium Basics & Syntax
Which of the following cannot be inherited in Java?
A Public methods
B Protected methods
C Private methods
D Static methods can be inherited
Correct Answer:  C. Private methods
EXPLANATION

Private members are not inherited. They remain accessible only within the class they are declared in.

Test
Q.26 Medium Basics & Syntax
Which of the following about static variables is incorrect?
A Static variables are shared among all instances
B Static variables are initialized when class loads
C Each object has its own copy of static variables
D Static variables can be accessed using class name
Correct Answer:  C. Each object has its own copy of static variables
EXPLANATION

Static variables are shared among ALL instances of a class, not individual copies per object.

Test
Q.27 Medium Basics & Syntax
What is the output of: System.out.println(5 % 2 + 3 * 2 - 1);?
A 6
B 7
C 8
D 10
Correct Answer:  A. 6
EXPLANATION

Following operator precedence: 5%2=1, 3*2=6, then 1+6-1=6

Test
Q.28 Medium Basics & Syntax
Which statement about constructor overloading is true?
A Constructors cannot be overloaded
B Constructors can be overloaded with different parameter types and counts
C Only one constructor is allowed per class
D Constructor return type must differ for overloading
Correct Answer:  B. Constructors can be overloaded with different parameter types and counts
EXPLANATION

Constructors can be overloaded based on parameter count and types, but not on return type.

Test
Q.29 Medium Basics & Syntax
Which access modifier allows access within the same package and subclasses?
A public
B private
C protected
D default
Correct Answer:  C. protected
EXPLANATION

protected allows access within the same package and to subclasses even outside the package.

Test
Q.30 Medium Basics & Syntax
What is the difference between == and .equals() for String comparison?
A Both check string content
B == checks reference, .equals() checks content
C == checks content, .equals() checks reference
D They are identical in functionality
Correct Answer:  B. == checks reference, .equals() checks content
EXPLANATION

== compares object references (memory location), while .equals() compares the actual string content.

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