Java Programming
Java OOP, collections, multithreading
958 Questions 10 Topics Take Test
Advertisement
Showing 941–950 of 958 questions
Q.941 Easy Basics & Syntax
What will be the output of: System.out.println(5 / 2);
A 2.5
B 2
C 2.0
D Error
Correct Answer:  B. 2
EXPLANATION

Integer division in Java results in an integer. 5/2 = 2 (remainder is discarded). To get 2.5, at least one operand must be float/double.

Take Test
Q.942 Hard Basics & Syntax
Consider: int x = 0; System.out.println(x == 0 && x != 1); What is the output?
A true
B false
C Compilation error
D 1
Correct Answer:  A. true
EXPLANATION

Both conditions are true: x == 0 (true) and x != 1 (true). The && (AND) operator returns true when both operands are true.

Take Test
Q.943 Easy Basics & Syntax
What will be the output of: boolean flag = true; System.out.println(!flag);
A true
B false
C !true
D Compilation error
Correct Answer:  B. false
EXPLANATION

The logical NOT operator (!) inverts the boolean value. !true becomes false.

Take Test
Q.944 Medium Basics & Syntax
Which of the following is the correct way to declare and initialize an array in Java?
A int array[5] = {1, 2, 3, 4, 5};
B int[] array = {1, 2, 3, 4, 5};
C int array = [1, 2, 3, 4, 5];
D array[5] int = {1, 2, 3, 4, 5};
Correct Answer:  B. int[] array = {1, 2, 3, 4, 5};
EXPLANATION

The correct syntax is 'datatype[] arrayName = {elements};' or 'datatype[] arrayName = new datatype[size];'

Take Test
Q.945 Hard Basics & Syntax
What is the scope of a local variable in Java?
A Throughout the class
B Throughout the method or block where it is declared
C Throughout the package
D Throughout the entire program
Correct Answer:  B. Throughout the method or block where it is declared
EXPLANATION

Local variables have scope limited to the method or code block in which they are declared. They cannot be accessed outside that block.

Take Test
Advertisement
Q.946 Medium Basics & Syntax
Identify the output: System.out.println("Java".length());
A 4
B 5
C Compilation error
D null
Correct Answer:  A. 4
EXPLANATION

The length() method returns the number of characters in the string "Java", which is 4 characters.

Take Test
Q.947 Hard Basics & Syntax
What will be printed? System.out.println(10 + 20 + "Java");
A 30Java
B 10 + 20 + Java
C 1020Java
D Compilation error
Correct Answer:  A. 30Java
EXPLANATION

String concatenation works left to right. 10 + 20 = 30 (integer addition), then 30 + "Java" = "30Java" (string concatenation).

Take Test
Q.948 Hard Basics & Syntax
Consider the code: int a = 5; int b = a++; System.out.println(a + " " + b); What is the output?
A 5 5
B 6 5
C 5 6
D 6 6
Correct Answer:  B. 6 5
EXPLANATION

The post-increment operator (a++) returns the value before incrementing. So b = 5, then a becomes 6. Output: 6 5

Take Test
Q.949 Medium Basics & Syntax
Which statement is correct about Java variables?
A Variable names are case-sensitive
B Variable names can start with a digit
C Variable names can contain spaces
D Variable names can use special characters like @, #
Correct Answer:  A. Variable names are case-sensitive
EXPLANATION

Java variable names are case-sensitive. 'age' and 'Age' are different variables. Names cannot start with digits, cannot contain spaces, and special characters are not allowed.

Take Test
Q.950 Medium Basics & Syntax
What is the output of: System.out.println(5 > 3 ? 'A' : 'B');
A A
B B
C true
D Compilation error
Correct Answer:  A. A
EXPLANATION

The ternary operator (?:) evaluates the condition (5 > 3, which is true) and returns the first value 'A'.

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