Java Programming — Basics & Syntax
Java OOP, collections, multithreading
34 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 34 questions in Basics & Syntax
Which of the following is a valid declaration of a two-dimensional array in Java that can store 3 rows and 4 columns of integers?
A int arr[][] = new int[3][4];
B int[] arr[] = new int[3][4];
C int[][] arr = new int[3][4];
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

All three declarations are syntactically valid in Java and represent the same 2D array structure. The position of brackets doesn't matter for 2D array declaration - they all create a 3x4 integer array.

Take Test
Consider the following code. What will be printed?
String s = "Java";
s = s.concat(" Programming");
System.out.println(s.length());
A 4
B 8
C 17
D 19
Correct Answer:  C. 17
EXPLANATION

"Java" has 4 characters. After concatenation with " Programming" (12 characters including space), the total length is 4 + 13 = 17 characters.

Take Test
What will happen when you execute the following code?
int[] arr = new int[5];
System.out.println(arr[5]);
A Output: 0
B Output: null
C ArrayIndexOutOfBoundsException at runtime
D Compilation error
Correct Answer:  C. ArrayIndexOutOfBoundsException at runtime
EXPLANATION

An array of size 5 has valid indices from 0 to 4. Accessing arr[5] throws ArrayIndexOutOfBoundsException at runtime because index 5 is out of bounds.

Take Test
Which of the following statements about Java's garbage collection is TRUE?
A Garbage collection is guaranteed to run at specific time intervals
B The programmer must explicitly call System.gc() to free memory
C Garbage collection automatically reclaims memory used by unreferenced objects
D Java does not have automatic garbage collection mechanism
Correct Answer:  C. Garbage collection automatically reclaims memory used by unreferenced objects
EXPLANATION

Java's garbage collector automatically identifies and reclaims memory of objects that are no longer referenced. While System.gc() can be suggested, it's not guaranteed to execute immediately.

Take Test
Consider the following code snippet. What is the primary purpose of the 'final' keyword when applied to a class in Java?
A Prevents the class from being instantiated
B Prevents the class from being inherited by other classes
C Makes all methods in the class abstract
D Allows the class to be accessed globally without import
Correct Answer:  B. Prevents the class from being inherited by other classes
EXPLANATION

The 'final' keyword when applied to a class prevents it from being extended or inherited. This is a fundamental OOP concept in Java used to restrict inheritance.

Take Test
Advertisement
What is the output of: System.out.println(5 > 3 ? "Yes" : "No");?
A 5
B 3
C Yes
D No
Correct Answer:  C. Yes
EXPLANATION

This uses the ternary operator (conditional operator). The condition 5 > 3 is true, so it returns the value after the '?', which is "Yes".

Take Test
Which of the following is NOT a Java keyword?
A synchronized
B volatile
C thread
D transient
Correct Answer:  C. thread
EXPLANATION

'thread' is not a Java keyword. 'synchronized', 'volatile', and 'transient' are all valid Java keywords used for special purposes.

Take Test
Which of the following is true about the 'break' statement?
A It terminates the entire program
B It skips the current iteration of a loop
C It exits the current loop or switch statement
D It jumps to the next iteration
Correct Answer:  C. It exits the current loop or switch statement
EXPLANATION

'break' statement exits the innermost loop or switch statement. It doesn't skip the current iteration (that's 'continue') and doesn't terminate the entire program.

Take Test
What is the output of: System.out.println(10 / 3);?
A 3.333...
B 3.33
C 3
D 4
Correct Answer:  C. 3
EXPLANATION

When dividing two integers, Java performs integer division and returns an integer result. 10 / 3 = 3 (remainder is discarded). To get decimal result, at least one operand should be a float or double.

Take Test
Q.10 Easy Basics & Syntax
What is the size of 'long' data type in Java?
A 16 bits
B 32 bits
C 64 bits
D Platform dependent
Correct Answer:  C. 64 bits
EXPLANATION

In Java, the 'long' data type is always 64 bits, regardless of the platform. This is one of Java's strengths - its size specifications are platform-independent.

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