wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

MCQs on Java for Grade 8

Total questions: 50

Worksheet time: 25mins

Name
Class
Date
1.

Which of the following is used to print output in Java?

a)

print()

b)

println()

c)

printf()

d)

All of the above

2.

What does System.out.println("Hello"); do?

a)

Prints Hello and stays on same line

b)

Prints Hello and moves to next line

c)

Prints nothing

d)

Gives an error

3.

Which statement prints text without moving to next line?

a)

System.out.println()

b)

System.out.print()

c)

System.out.printf()

d)

None of these

4.

What is the output of System.out.print("Hi "); System.out.print("There");?

a)

Hi

b)

There

c)

Hi There

d)

HiThere

5.

Which of these is NOT a valid way to display output?

a)

System.out.print("Hello")

b)

System.out.println("Hello")

c)

System.print("Hello")

d)

System.out.printf("Hello")

6.

Which of these is a correct way to declare an integer variable?

a)

int x;

b)

x int;

c)

integer x;

d)

var int x;

7.

Which data type is used to store decimal numbers?

a)

int

b)

float

c)

String

d)

char

8.

Which data type is used for very precise decimal values?

a)

int

b)

float

c)

double

d)

char

9.

Which of these is used to store text?

a)

char

b)

String

c)

int

d)

double

10.

What is the size of int in Java?

a)

2 bytes

b)

4 bytes

c)

8 bytes

d)

1 byte

11.

Which of these is a valid variable name?

a)

1number

b)

number_1

c)

int

d)

double

12.

What is the default value of int if not initialized?

a)

0

b)

null

c)

0.0

d)

garbage value

13.

Which keyword is used to declare a floating-point variable?

a)

int

b)

double

c)

float

d)

char

14.

How do you assign value 10 to an integer variable x?

a)

int x = 10;

b)

x = int 10;

c)

x := 10;

d)

int x := 10;

15.

Which is correct declaration of a String variable?

a)

string name = "John";

b)

String name = "John";

c)

str name = "John";

d)

char name = "John";

16.

How do you print variable x having value 5?

a)

System.out.println(x);

b)

print(x);

c)

System.out.print("x");

d)

printf(x);

17.

How do you print text and variable together?

a)

System.out.println("Value: " + x);

b)

System.out.println("Value: ", x);

c)

System.out.print("Value:" x);

d)

print("Value:" + x);

18.

What is the output of: int x=10; System.out.print(x);?

a)

x

b)

10

c)

Error

d)

nothing

19.

What does + do in a print statement?

a)

Adds numbers only

b)

Joins strings only

c)

Works for both addition and joining

d)

None

20.

How do you display int a=5 and int b=10 as 5 10?

a)

System.out.println(a b);

b)

System.out.println(a + b);

c)

System.out.println(a + " " + b);

d)

System.out.println("a b");

21.

Which package must you import to use Scanner?

a)

java.util.Scanner

b)

java.io.Scanner

c)

java.Scanner

d)

scanner.util

22.

How do you create Scanner object?

a)

Scanner sc = new Scanner();

b)

Scanner sc = Scanner();

c)

Scanner sc = new Scanner(System.in);

d)

Scanner sc = new scanner(System.in);

23.

Which method reads an integer input?

a)

nextInt()

b)

nextInteger()

c)

getInt()

d)

readInt()

24.

Which method reads a whole line of text?

a)

next()

b)

nextLine()

c)

readLine()

d)

nextText()

25.

Which method reads a single word?

a)

next()

b)

nextLine()

c)

nextWord()

d)

readWord()

26.

What does System.in represent?

a)

Output stream

b)

Input stream

c)

Error stream

d)

File stream

27.

Which of these is correct?

a)

int x = sc.nextInt();

b)

String s = sc.next();

c)

float f = sc.nextFloat();

d)

All of the above

28.

If you forget to import Scanner, what happens?

a)

No error

b)

Compile-time error

c)

Run-time error

d)

Warning only

29.

What is the correct sequence to read an integer?

a)

Declare variable → Create Scanner → Read input

b)

Read input → Create Scanner → Declare variable

c)

Read input → Declare variable → Create Scanner

d)

None of the above

30.

What happens if you enter a letter when nextInt() is called?

a)

Reads as 0

b)

Runtime error

c)

Reads as string

d)

Skips

31.

Which is the correct syntax of if statement?

a)

if {condition} {statements}

b)

if (condition) {statements}

c)

if condition {statements}

d)

if [condition] {statements}

32.

What is the output of: if(5>3) System.out.print("Yes");?

a)

Yes

b)

No

c)

Error

d)

Nothing

33.

What does else mean?

a)

Executes if condition is true

b)

Executes if condition is false

c)

Executes always

d)

Does nothing

34.

What is the output: int x=10; if(x<5) System.out.print("Small"); else System.out.print("Big");?

a)

Small

b)

Big

c)

Error

d)

Nothing

35.

Can if exist without else?

a)

Yes

b)

No

36.

Can else exist without if?

a)

Yes

b)

No

37.

What is the output: int x=5; if(x==5) System.out.print("Equal"); else System.out.print("Not Equal");?

a)

Equal

b)

Not Equal

38.

Which operator is used for equality check?

a)

=

b)

==

c)

!=

d)

equals()

39.

Which operator is used for NOT equal?

a)

=

b)

==

c)

!=

d)

<>

40.

How many if…else if…else can be chained?

a)

Only one

b)

Only two

c)

Any number

d)

None

41.

Which of these is true?

a)

if-else is used for decisions

b)

if-else is used for loops

c)

if-else is used for output

d)

if-else is used for input

42.

Which is valid?

a)

if(x=5)

b)

if(x==5)

c)

if x=5

d)

if x==5

43.

What happens if no condition is true and no else is given?

a)

Error

b)

Nothing happens

c)

Executes next statement

d)

Both b and c

44.

Which is correct nested if?

a)

if(x>0) if(x<10) System.out.print(x);

b)

if(x>0) then if(x<10)

c)

if(x>0 and x<10)

d)

if(x>0: if(x<10))

45.

What is the output: int x=7; if(x>5) if(x<10) System.out.print("OK");?

a)

OK

b)

Error

c)

Nothing

46.

What is the output: if(false) System.out.print("Yes"); else System.out.print("No");?

a)

Yes

b)

No

47.

Which of these can replace if-else in some cases?

a)

while

b)

switch

c)

for

d)

do-while

48.

What is evaluated in an if condition?

a)

integer

b)

float

c)

boolean

d)

string

49.

Which of these is invalid?

a)

if(true)

b)

if(false)

c)

if(0)

d)

if(x>0)

50.

What is the output: if(5<3) System.out.print("Yes"); else System.out.print("No");?

a)

Yes

b)

No