Top 50 Java MCQ Questions

Top 50 Java MCQ Questions

12 Jun 2024
897 Views
21 min read
Learn via Video Course & by Doing Hands-on Labs

Java Programming For Beginners Free Course

Java MCQs are a good way to practice and become a master in Java Programming. Java is a high-level object-oriented programming language. It is easy to understand and used to develop programs. It can be used in Android development as well as web development.

 In this Java tutorial, I brought some multiple-choice questions and answers on Java programming language only for you, definitely, It will help you to prepare well for your exams, or interviews. So let's explore the top 50 Java MCQ questions and answers.

1. Which of these is not a feature of Java?

    A.Object-oriented

    B.Platform-independent

    C.Compiled

    D.Interpreted language

    Answer: C Compiled

2. Which component of Java is responsible for running the compiled Java bytecode?

    A.JDK

    B.JVM

    C.JRE

    D.JIT

    Answer: B.JVM

3. What is the purpose of the PATH environment variable in Java?

    A.To locate Java libraries

    B.To store Java bytecode

    C. To Locate the exe files (.exe)

    D.To optimize Java code

    Answer: C. To Locate the exe files (.exe)

4. Which feature of Java makes it possible to run a Java program on different platforms?

    A.Object-Oriented

    B.Platform-Independent

    C.Syntax

    D.Memory Management

    Answer: B.Platform-Independent

5. In Java, how should class names be written?

    A.camelCase

    B.snake_case

    C.PascalCase

    D.kebab-case

    Answer: C.PascalCase

6. How do I comment a block of code in Java?

    A./*comment*/

    B.//comment

    C.!--comment--

    D.%comment

    Answer: A./*comment*/

7. Which data type would be best for storing a person's age in Java?

    A.int

    B.double

    C.long

    D.byte

    Answer: A.int

8. What is the default value of a boolean variable in Java?

    A.true

    B.false

    C.0

    D.null

    Answer: B.false

9. What is the result of this operation in Java?

(int)(7.9)   

    A.7

    B.7.9

    C.8

    D.Syntax Error

    Answer: A.7

10. Which keyword is used to define a constant variable in Java?

    A.final

    B.static

    C.const

    D.immutable

    Answer: A.final

11. What is the range of the short data type in Java?

    A.-32768 to 32767

    B.-128 to 127

    C.-2147483648 to 2147483647

    D.0 to 65535

    Answer: A.-32768 to 32767

12. What will be the output of the following code snippet?

int a = 10; int b = 20; 
System.out.println(a + b);

    A.10

    B.20

    C.30

    D.Error

    Answer: C.30

13. Identify the output.

boolean isJavaFun = true; System.out.println(!isJavaFun);    

    A.true

    B.false

    C.Error

    D.null

    Answer: B.false

14. Which one of the following is true for Java

    A. Java is object-oriented and interpreted

    B. Java is efficient and faster than C

    C. Java is the choice of everyone.

    D. Java is not robust.

    Answer: A. Java is object-oriented and interpreted

15. What is the output of this pseudocode?

SET x = 10
IF x > 5
THEN PRINT "Greater"
ELSE PRINT "Lesser"    
    A.Greater
    B.Lesser
    C.Error
    D.No output

    Answer: A.Greater

16. Evaluate this pseudocode.

SET a = 3 SET b = 4 PRINT a * b    

    A.7

    B.12

    C.Error.

    D.None of the above

Answer: B.12

17. Determine the output.

SET num = 4 IF num MOD 2 = 0 THEN PRINT "Even" ELSE PRINT "Odd"   

    A.Even

    B.Odd

    C.Error

    D.No output

Answer: A.Even

18. Identify the error in this code.

int[] nums = new int[2]; 
nums[0] = 1; 
nums[1] = 2; 
nums[2] = 3;    

    A.Array index out of bounds

    B.Incorrect array declaration

    C.No error

    D.Syntax error

Answer: A.Array index out of bounds

19. Spot the mistake in this code snippet.

int i = 0; while(i < 5) { i++; } System.out.println(i);    

    A.Infinite loop

    B.Syntax error

    C.No error

    D.Prints 0

Answer:C.No error

20. Which control structure is used to execute a block of code multiple times?

    A.if

    B.switch-case

    C.for

    D.try-catch

Answer: C.for

21. What will be the output of the following code snippet.

if(false){ System.out.println("True"); } else{ System.out.println("False"); }  

    A.True

    B.False

    C.Error

    D.No Output

Answer: B.False

22. In a 'switch-case' statement, what is the role of the 'break' keyword?

    A. To pause the execution

    B. To terminate the case block

    C. To skip to the next case

    D. To repeat the case block

Answer: B.To terminate the case block

23. What is the difference between 'while' and 'do-while' loops in Java?

    A.Syntax only

    B.The 'do-while' loop executes at least once

    C.Execution speed

    D.No difference

Answer:B.The 'do-while' loop executes at least once

24. Which keyword is used to exit a loop prematurely in Java?

    A.break

    B.continue

    C.exit

    D.stop

Answer: A.break

25. What will the following loop print?

for(int i = 0; i < 4; i++)
{ 
System.out.println(i);
}   

    A.0 1 2

    B.1 2 3

    C.0 1 2 3

    D.None

Answer: A.0 1 2

26. What is the output of this code?

int x = 1; 
while(x < 4)
{ 
System.out.println(x);
 x++; 
}

    A.1 2 3

    B.2 3 4

    C.1 2 3 4

    D.1

Answer:A.1 2 3

27. What will this pseudocode output?

SET count = 5 
DO PRINT count 
COUNTDOWN count

    A.5 4 3 2 1

    B.5

    C.1 2 3 4 5

    D.None

Answer:A.5 4 3 2 1

28. Analyze this pseudocode.

SET num = 0 
WHILE num <= 5 
IF num MOD 2 = 0 
THEN PRINT num END 
IF INCREMENT num

    A.0 2 4

    B.0 1 2 3 4 5

    C.2 4

    D.0 2 4 6

Answer:A.0 2 4

29. Identify the error.

for(int i=0; i<=5; i++) 
{ 
System.out.println(i); 
} 
System.out.println(i);

    A.Syntax error outside the loop

    B.No error

    C.Variable i not accessible outside loop

    D.Infinite loop

Answer:C.Variable i not accessible outside the loop

30. Spot the mistake.

int counter = 0;
while(counter < 5)
{
counter++;
}
System.out.println("Count: " + counter);

    A.Loop never terminates

    B.Counter not incremented

    C.No error

    D.Print statement incorrect

Answer:C.No error

31. Find the error in this code.

int num = 1; 
do{ 
System.out.println(num); 
num++; 
} while(num <= 3); 
System.out.println(num);

    A.Infinite loop

    B.Syntax error

    C.Prints wrong values

    D.No error

Answer:D.No error

32. Which class is commonly used for simple keyboard input in Java?

    A.Scanner

    B.InputStream

    C.BufferedReader

    D.FileReader

Answer: A.Scanner

33. In Java, what is the default value of an array of integers?

    A.0

    B.null

    C.1

    D.-1

Answer: A.0

34. What does 'BufferedReader' in Java provide that 'Scanner' does not?

    A.Faster input reading

    B.Input from file only

    C.Different data types

    D.Graphical interface

Answer:A.Faster input reading

35.How do you access the third element in an array named 'arr'?

    A.arr[2]

    B.arr[3]

    C.arr(2)

    D.arr(3)

Answer:A.arr[2]

36. How do you access the third element in an array named 'arr'?

    A.Compile-time error

    B.Runtime error: ArrayIndexOutOfBoundsException

    C.No error

    D.Logical error

Answer: B.Runtime error: ArrayIndexOutOfBoundsException

37. What is the purpose of the 'length' attribute in a Java array?

    A. To determine the size of the array

    B. To modify the size of the array

    C. To sort the array

    D. To initialize the array

Answer: A.To determine the size of the array

38. Which of these is not a valid way to instantiate an array in Java?

    A.int[] arr = new int[5];

    B.int arr[] = new int[5];

    C.int arr[] = {1, 2, 3, 4, 5};

    D.int arr[] = int[5];

Answer:int arr[] = int[5];

39.In a multi-dimensional array, how do you access the element in the second row and third column of an array named 'matrix'?

    A.matrix[1][2]

    B.matrix[2][3]

    C.matrix[2][2]

    D.matrix[1][3]

Answer:A.matrix[1][2]

40. What will the following code output.

int[] arr = {1, 2, 3}; 
for(int num : arr) 
{
 System.out.println(num);
 }

    A.1 2 3

    B.123

    C.Error

    D.None

Answer:A.1 2 3

41. What does this Java code do?


int[][] arr = {{1, 2}, {3, 4}}; 
for(int i = 0; i < arr.length; i++) 
{ 
for(int j = 0; j < arr[i].length; j++) 
{ 
System.out.print(arr[i][j] + " "); 
} 
System.out.println(); }

    A.Prints a 2D array in matrix form

    B.Prints only the first row of the array

    C.Prints the sum of the array elements

    D.Gives an error

Answer: A.Prints a 2D array in matrix form

42. What will be the result of executing this code snippet?

String[] names = {"Java", "Python", "C++"}; 
System.out.println(names[1].length());

    A.4

    B.5

    C.6

    D.Error

Answer: C.6

43. Analyze this code.

int[] nums = new int[3];
nums[1] = 10;
int x = nums[1] + nums[0]; 
System.out.println(x);

    A.0

    B.10

    C.Error

    D.None

Answer: B.10

44. What will this pseudocode output?

SET arr = [1, 2, 3] FOR EACH num IN arr PRINT num

    A.1 2 3

    B.123

    C.Error

    D.None

Answer: A.1 2 3

45. Determine the output of this pseudocode.

SET arr = [10, 20, 30] SET sum = 0 FOR i = 0 TO LENGTH(arr) - 1 INCREMENT sum BY arr[i] PRINT sum

    A.60

    B.1020 30

    C.Error

    D.None

Answer: A.60

46. What will be the result of this pseudocode?

SET matrix = [[1, 2], [3, 4]]
PRINT matrix[0][1]

    A.1

    B.2

    C.3

    D.4

Answer:B.2

47.Identify the issue in this code snippet.

int[] numbers = new int[5];
for(int i = 0;
i <= numbers.length;i++) {System.out.println(numbers[i]);
}

    A.Out of bounds array access

    B.No issue

    C.Syntax error

    D.Logic error

Answer:A.Out of bounds array access

48.Spot the mistake in this code.

String[] names = {"Java", "Python", "C"};
for(int i = 0;
i < names.length;
i++) {
System.out.println(names[i].length());
}

    A.Prints incorrect lengths

    B.No error

    C.Syntax error

    D.Logic error

Answer:B.No error

49.Find the error in this Java code.

int[][] matrix = new int[2][2];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[1][0] = 3;
System.out.println(matrix[1][1]);

    A.Prints incorrect value

    B.No error

    C.Syntax error

    D.Missing initialization for matrix[1][1]

Answer:D.Missing initialization for matrix[1][1]

50.Identify the flaw in this Java code.

char[] chars = new char[-1];

    A.Negative array size

    B.Syntax error

    C.No error

    D.Logic error

Answer: A.Negative array size

Conclusion:

So, here we have covered the mostly asked Java MCQ Questions from basic to advanced for all interested candidates. For a complete understanding of Java refer to our Java Certification Program.

FAQs

Q1. Who is the godfather of Java?

James Gosling is considered the father of Java.

Q2. What is Java full form?

JAVA stands for Just Another Virtual Accelerator.

Q3. Why is Java called JVM?

It functions as a virtual machine, translating Java bytecode into machine language.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course Jun 16 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
.NET Microservices Certification Training Jun 23 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this