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


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


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

2

3

5


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

int

float

string

char


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

3

5

7


7. Which of the following is not a valid way to declare and initialize a String variable in Java?

String var = "hello";

String var = new String("hello");

String var = 'hello';

String var = "hello".toUpperCase();


8.

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


9.

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


10.

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