menu

Java Basic Programs for Beginners


1. Which of the following is a valid way to create a new object in Java?

Object obj = new Object();

Object obj = Object();

Object obj = new Object;

Object obj = Object;


2. Which of the following is not a valid way to declare and initialize an integer array in Java?

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

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

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

int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3;


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

1

0

Compile error

Runtime error


4.

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


5.

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!


6.

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

1


7.

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


8.

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


9.

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


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