Home Subjects Java Programming Basics & Syntax

Java Programming
Basics & Syntax

Java OOP, collections, multithreading

34 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 34
Topics in Java Programming
Q.11 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.

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

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

Test
Q.14 Easy Basics & Syntax
Which of the following is a valid method name in Java?
A 2ndMethod()
B _myMethod()
C my-method()
D my method()
Correct Answer:  B. _myMethod()
EXPLANATION

Method names must start with a letter, underscore, or dollar sign. '_myMethod()' is valid. Names cannot start with digits, contain hyphens, or spaces.

Test
Q.15 Easy Basics & Syntax
Consider the code: int x = 5; x += 3; What is the value of x?
A 5
B 8
C 3
D 53
Correct Answer:  B. 8
EXPLANATION

The compound assignment operator += means x = x + 3. So 5 + 3 = 8.

Test
Q.16 Easy Basics & Syntax
What will be the output of: System.out.println(10 / 3);
A 3.33
B 3
C 3.333333
D Compilation Error
Correct Answer:  B. 3
EXPLANATION

Both operands are integers, so integer division is performed. Result is 3 (not 3.33). To get decimal, at least one operand must be float/double.

Test
Q.17 Easy Basics & Syntax
What is the purpose of the 'instanceof' operator?
A To create new instances
B To check if an object is an instance of a class
C To compare object values
D To delete object instances
Correct Answer:  B. To check if an object is an instance of a class
EXPLANATION

The 'instanceof' operator is used to check if an object is an instance of a specific class or implements a specific interface.

Test
Q.18 Easy Basics & Syntax
What does the 'super' keyword do in Java?
A Refers to the current object
B Refers to the parent class
C Creates a new instance
D Marks a method as superior
Correct Answer:  B. Refers to the parent class
EXPLANATION

The 'super' keyword is used to refer to parent class members and invoke parent class constructors.

Test
Q.19 Easy Basics & Syntax
Which of the following is a valid variable declaration?
A int 2num;
B int num-2;
C int num$2;
D int num.2;
Correct Answer:  C. int num$2;
EXPLANATION

Variable names can contain letters, digits, underscores, and dollar signs, but cannot start with a digit or contain hyphens or dots.

Test
Q.20 Easy Basics & Syntax
What is the correct way to declare a constant in Java?
A const int MAX = 100;
B static int MAX = 100;
C public static final int MAX = 100;
D final static int MAX = 100;
Correct Answer:  C. public static final int MAX = 100;
EXPLANATION

'const' is not a Java keyword. The correct convention for constants is public static final. Option D also works but C follows the standard convention.

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