Create New Post

Java MCQs - 6

  1. What is the output of the following code?

 

int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(arr[1][2]);

A) 2
B) 5
C) 6
D) 7

Answer: C) 6

  1. In Java, which of the following is NOT a valid way to declare an array?

A) int[] arr = new int[5];
B) int arr[] = {1, 2, 3, 4, 5};
C) int[5] arr;
D) int[][] arr = new int[3][3];

Answer: C) int[5] arr;

  1. What is the output of the following code?
String str = "hello";
str.concat(" world");
System.out.println(str);

A) hello
B) world
C) hello world
D) Compiler error

Answer: A) hello

Explanation: The concat() method returns a new string and does not modify the original string.

  1. Which of the following statements about abstract classes in Java is true?

A) Abstract classes cannot have constructors.
B) Abstract classes can be instantiated using the 'new' keyword.
C) Abstract classes cannot contain abstract methods.
D) Abstract classes can be marked as final.

Answer: A) Abstract classes cannot have constructors.

  1. What is the output of the following code?
int x = 5;
System.out.println(x << 1);

A) 10
B) 5
C) 2
D) 20

Answer: A) 10

Explanation: The << operator is the left shift operator which shifts the bits of the number to the left by the specified number of positions.

  1. Which of the following statements about Java packages is true?

A) A package can only contain classes but not interfaces.
B) A package must have the same name as the directory in which it is stored.
C) A package can be directly executed using the 'java' command.
D) Packages provide a way to organize and group related classes and interfaces.

Answer: D) Packages provide a way to organize and group related classes and interfaces.

  1. What is the output of the following code?
System.out.println(Math.max(10, 20));

A) 10
B) 20
C) 30
D) Compiler error

Answer: B) 20

Explanation: The Math.max() method returns the greater of two values.

  1. Which of the following statements about Java annotations is true?

A) Annotations are used to specify the access modifiers of a class or method.
B) Annotations are comments that are ignored by the compiler.
C) Annotations can be created using the 'annotate' keyword.
D) Annotations provide metadata about a program that can be used by the compiler or at runtime.

Answer: D) Annotations provide metadata about a program that can be used by the compiler or at runtime.

  1. What is the output of the following code?
System.out.println("Hello".charAt(1));

A) H
B) e
C) l
D) Compiler error

Answer: B) e

  1. Which of the following statements about Java threads is true?

A) A thread is a lightweight process.
B) Java does not support multithreading.
C) Threads are always executed sequentially.
D) Threads cannot communicate with each other.

Answer: A) A thread is a lightweight process.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

74432