menu

Java Basics


1. Which of the following is a valid way to declare an array in Java?

int[] numbers = {1, 2, 3};

int numbers[] = {1, 2, 3};

int numbers = {1, 2, 3};

Both A and B


2. Which of the following is NOT a valid access modifier in Java?

public

private

protected

static


3.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
int x = 10;
int y = 5;
if(x > y) {
System.out.println("x is greater than y");
} else {
System.out.println("x is less than or equal to y");
  }
 }
}

x is greater than y

x is less than or equal to y

Both statements are printed

No output


4.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
int x = 10;
int y = 20;
System.out.println(x < y && x > 5);
 }
}

1

0

1

0


5. Which of the following is NOT a loop in Java?

while

for

do-while

if


6. Which of the following is a loop statement in Java?

if

switch

for

try


7.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
int x = 5;
System.out.println(x++);
 }
}

4

5

6

7


8. Which of the following is NOT a valid type of inheritance in Java?

Single

Multiple

Hierarchical

Hybrid


9.

What is the output of the following code?

public class Test {
public static void main(String args[]) {
for(int i = 0; i < 5; i++) {
System.out.println(i);
  }
 }
}

0 1 2 3 4

1 2 3 4 5

0 1 2 3 4 5

Infinite loop


10. Which of the following is NOT a primitive data type in Java?

int

boolean

double

string