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 21–30 of 34
Topics in Java Programming
Q.21 Easy Basics & Syntax
Which of the following statements about Java packages is correct?
A Package names must be in uppercase
B Package names use dot notation for hierarchy
C A single Java file can belong to multiple packages
D Package statement must appear after import statements
Correct Answer:  B. Package names use dot notation for hierarchy
EXPLANATION

Package names follow a hierarchical structure using dots (e.g., com.example.app). Package statement must be the first non-comment statement.

Test
Q.22 Easy Basics & Syntax
Which keyword is used to prevent method overriding in Java?
A abstract
B final
C static
D synchronized
Correct Answer:  B. final
EXPLANATION

The 'final' keyword prevents a method from being overridden by subclasses.

Test
Q.23 Easy Basics & Syntax
Which of the following is NOT a valid Java identifier?
A _variable123
B $myVar
C 123variable
D my_Var$
Correct Answer:  C. 123variable
EXPLANATION

Java identifiers cannot start with a digit. Valid identifiers must start with a letter, underscore, or dollar sign.

Test
Q.24 Easy Basics & Syntax
What will be the output of: System.out.println(Math.max(5, 10) - Math.min(3, 8));
A 2
B 5
C 7
D 15
Correct Answer:  C. 7
EXPLANATION

Math.max(5, 10) = 10, Math.min(3, 8) = 3. So 10 - 3 = 7.

Test
Q.25 Easy Basics & Syntax
What will be the output: String s = "Hello"; System.out.println(s.charAt(1));
A H
B e
C l
D o
Correct Answer:  B. e
EXPLANATION

charAt() uses 0-based indexing. Index 1 refers to the second character, which is 'e' in "Hello".

Test
Q.26 Easy Basics & Syntax
Which of the following is the correct syntax for a multi-line comment in Java?
A // This is a comment
B /* This is a comment */
C
D # This is a comment
Correct Answer:  B. /* This is a comment */
EXPLANATION

Multi-line comments in Java use /* */ syntax. Single-line comments use //. Other syntaxes are for different languages.

Test
Q.27 Easy Basics & Syntax
What will be printed? float f = 5.5f; System.out.println(f);
A 5.5
B 5.50
C Error
D 5
Correct Answer:  A. 5.5
EXPLANATION

The 'f' suffix indicates a float literal. The output will be 5.5 (trailing zeros are not shown in standard printing).

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

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

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

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