menu

Java Basic Programs for Beginners


1. Which of the following is a method of the String class?

length()

size()

count()

amount()


2.

What is the output of the following program?

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

10

12

14

16


3.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
  }
 }
}

1 2 3 4 5

5 4 3 2 1

1 3 5

2 4


4. Which of the following is not a control structure in Java?

if

for

switch

call


5.

What is the output of the following program?

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

2

3

5

7


6. Which of the following is a valid way to declare a constant in Java?

final int var = 5;

int const var = 5;

const int var = 5;

int var = 5; final


7. Which of the following operators has the highest precedence in Java?

*

++

&&

+


8.

What is the output of the following program?

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

10

11

12

Compilation error


9. Which of the following is not a primitive data type in Java?

int

float

string

char


10. Which of the following is a valid way to declare and initialize a character array in Java?

char[] arr = {'a', 'b', 'c'};

char[] arr = new char[]{'a', 'b', 'c'};

char[] arr = new char[3]{'a', 'b', 'c'};

char[] arr = new char[3]; arr[0] = 'a'; arr[1] = 'b'; arr[2] = 'c';