menu

Java Basic Programs for Beginners


1.

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


2.

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 && y < x);
 }
}

1

0

Compile error

Runtime error


3.

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

-3


4.

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);
 }
}

1

0

Compile error

Runtime error


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.5

2

3

5


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

length()

size()

count()

amount()


7. Which of the following is a valid way to declare and initialize an array of integers in Java?

int[] arr = new int[5];

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

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

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


8.

What is the output of the following program?

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

1

0

Compile error

Runtime error


9. 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';


10.

What is the output of the following program?

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

1

0

Compile error

Runtime error