Java Programming — Basics & Syntax
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 31–40 of 100 questions in Basics & Syntax
Q.31 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.

Take Test
Q.32 Easy Basics & Syntax
Consider the following code: int x = 5; x += 3; System.out.println(x); What is the output?
A 5
B 8
C 53
D Compilation error
Correct Answer:  B. 8
EXPLANATION

The += operator is an assignment operator that adds the right operand to the left operand and assigns the result. So x += 3 means x = x + 3, which is 5 + 3 = 8.

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

Take Test
Q.34 Easy Basics & Syntax
What will be the result of: int a = 10; int b = 20; System.out.println(a + b + "Java");?
A "30Java"
B "1020Java"
C "Java30"
D Compilation error
Correct Answer:  A. "30Java"
EXPLANATION

When + operator is used with numeric values first, they are added (10 + 20 = 30), and then concatenated with the string "Java", resulting in "30Java".

Take Test
Q.35 Easy Basics & Syntax
Which keyword is used to create a constant variable in Java that cannot be modified after initialization?
A static
B const
C final
D immutable
Correct Answer:  C. final
EXPLANATION

The 'final' keyword is used to declare constants in Java. Once a final variable is assigned a value, it cannot be changed. 'const' is a reserved keyword but not used in Java.

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

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

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

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

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

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