menu

Java Basic Programs for Beginners


1. 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};


2.

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


3.

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


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

2.5

2

3

5


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

1

0

Compile error

Runtime error


6.

What is the output of the following program?

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

2.5

2

3

5


7. Which of the following is not a valid access modifier in Java?

private

public

protected

global


8.

What is the output of the following program?

public class Main {
public static void main(String[] args) {
String str = "Hello, world!";
System.out.println(str.toUpperCase());
 }
}

HELLO, WORLD!

Hello, world!

hello, world!

hELLO, WORLD!


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


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

double var = 3.14159;

double var = 3,14159;

double var = "3.14159";

double var = Double.parseDouble("3.14159");