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 11–20 of 50
Topics in Java Programming
Q.11 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.

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

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

Test
Q.14 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).

Test
Q.15 Medium Basics & Syntax
Which of the following correctly demonstrates method overloading?
A Two methods with same name and same parameters in different classes
B Two methods with same name but different number or type of parameters in the same class
C Two methods with different names and different parameters in the same class
D Two methods with same name and return type in the same class
Correct Answer:  B. Two methods with same name but different number or type of parameters in the same class
EXPLANATION

Method overloading allows multiple methods with the same name but different parameters (number, type, or order) in the same class. This is a way to implement compile-time (static) polymorphism.

Test
Q.16 Medium Basics & Syntax
What is the purpose of the 'this' keyword in Java?
A To refer to the parent class
B To refer to the current object instance
C To create a new object
D To access static variables
Correct Answer:  B. To refer to the current object instance
EXPLANATION

'this' is a reference variable that refers to the current object instance. It is used to distinguish instance variables from local variables with the same name, or to pass the current object as a parameter.

Test
Q.17 Medium Basics & Syntax
Which of the following is a feature of Java 8 that allows functional programming?
A Lambda expressions
B Generics
C Annotations
D Enums
Correct Answer:  A. Lambda expressions
EXPLANATION

Lambda expressions were introduced in Java 8, enabling functional programming. Syntax: (parameters) -> expression or statement

Test
Q.18 Medium Basics & Syntax
Consider: String s1 = "Java"; String s2 = new String("Java"); System.out.println(s1 == s2);
A true
B false
C Compilation Error
D Runtime Error
Correct Answer:  B. false
EXPLANATION

s1 is created in string pool, s2 is a new object in heap. Though content is same, references are different, so == returns false.

Test
Q.19 Medium Basics & Syntax
What is the difference between == and equals() in Java?
A Both are identical
B == compares values, equals() compares references
C == compares references, equals() compares values
D equals() is only for objects, == is for primitives
Correct Answer:  C. == compares references, equals() compares values
EXPLANATION

== compares object references (memory addresses). equals() method compares actual content/values and can be overridden.

Test
Q.20 Medium Basics & Syntax
Which statement about method parameters is correct?
A Parameters are passed by reference in Java
B Parameters are passed by value in Java
C Objects are passed by reference, primitives by value
D All parameters are passed by reference
Correct Answer:  B. Parameters are passed by value in Java
EXPLANATION

Java uses pass-by-value for all parameters. For objects, the value passed is the reference, but the reference itself is passed by value.

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