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 31–40 of 50
Topics in Java Programming
Q.31 Medium Basics & Syntax
Which statement about Java's 'this' keyword is correct?
A 'this' refers to the parent class
B 'this' refers to the current object instance
C 'this' is a static reference
D 'this' cannot be used in constructors
Correct Answer:  B. 'this' refers to the current object instance
EXPLANATION

'this' is a reference to the current object instance. It's used to distinguish instance variables from parameters and to call other constructors.

Test
Q.32 Medium Basics & Syntax
What is the output of: double d = 5.7; int i = (int) d; System.out.println(i);
A 6
B 5.7
C 5
D Error
Correct Answer:  C. 5
EXPLANATION

Explicit casting from double to int truncates the decimal part. 5.7 becomes 5 (not rounded).

Test
Q.33 Medium Basics & Syntax
What will be the output: int a = 10; int b = 20; System.out.println(a > b ? "A" : "B");
A A
B B
C Error
D AB
Correct Answer:  B. B
EXPLANATION

Ternary operator: condition ? trueValue : falseValue. Since 10 > 20 is false, "B" is printed.

Test
Q.34 Medium Basics & Syntax
Which of the following correctly initializes a 2D array in Java?
A int[][] arr = new int[3][4];
B int[] arr[] = new int[3][4];
C Both A and B
D Neither A nor B
Correct Answer:  C. Both A and B
EXPLANATION

Both syntaxes are valid in Java for declaring 2D arrays. The brackets can be placed after the type or variable name.

Test
Q.35 Medium Basics & Syntax
What is the output of: System.out.println("5" + 3 + 2);
A 10
B 532
C Error
D 7
Correct Answer:  B. 532
EXPLANATION

String concatenation: "5" + 3 becomes "53", then "53" + 2 becomes "532". Once a String is involved, + performs concatenation.

Test
Q.36 Medium Basics & Syntax
What is the output of: System.out.println(10 * 2 / 5 * 2);
A 8
B 4
C 16
D 2
Correct Answer:  A. 8
EXPLANATION

Operators *, / have same precedence and are evaluated left to right: ((10 * 2) / 5) * 2 = (20 / 5) * 2 = 4 * 2 = 8.

Test
Q.37 Medium Basics & Syntax
Which of the following statements will compile successfully?
A byte b = 300;
B byte b = (byte) 300;
C int i = 'a';
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Option A fails (300 exceeds byte range). Option B works with explicit casting. Option C works (char to int conversion is implicit).

Test
Q.38 Medium Basics & Syntax
What is the result of: System.out.println(true && false || true);
A true
B false
C Error
D null
Correct Answer:  A. true
EXPLANATION

&& has higher precedence than ||. So: (true && false) || true = false || true = true.

Test
Q.39 Medium Basics & Syntax
Which of the following correctly represents a long literal in Java?
A long num = 1000000;
B long num = 1000000L;
C long num = 1000000l;
D Both B and C
Correct Answer:  D. Both B and C
EXPLANATION

Both 'L' and 'l' suffixes are valid for long literals in Java, though 'L' is preferred as it's less confusing with '1'.

Test
Q.40 Medium Basics & Syntax
What is the output of: int x = 5; System.out.println(++x + x++);
A 11
B 12
C 10
D 13
Correct Answer:  B. 12
EXPLANATION

++x increments x to 6 and uses it, then x++ uses 6 and increments to 7. So 6 + 6 = 12.

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