Home Subjects Java Programming Basics & Syntax

Java Programming
Basics & Syntax

Java OOP, collections, multithreading

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 81–90 of 100
Topics in Java Programming
Q.81 Easy Basics & Syntax
Which of the following is a valid variable name in Java?
A 123abc
B _myVar
C my-Var
D class
Correct Answer:  B. _myVar
EXPLANATION

Variable names must start with a letter, underscore (_), or dollar sign ($). They cannot start with digits, contain hyphens, or be reserved keywords.

Take Test
Q.82 Easy Basics & Syntax
Which keyword is used to prevent a class from being inherited?
A static
B abstract
C final
D private
Correct Answer:  C. final
EXPLANATION

The 'final' keyword prevents a class from being extended/inherited. A final class cannot have subclasses.

Take Test
Q.83 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.84 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.85 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.86 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.87 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
Q.88 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.89 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.90 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
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