NEW
Font size
WorksheetsMCQs on Java for Grade 8
Total questions: 50
Worksheet time: 25mins
Which of the following is used to print output in Java?
print()
println()
printf()
All of the above
What does System.out.println("Hello"); do?
Prints Hello and stays on same line
Prints Hello and moves to next line
Prints nothing
Gives an error
Which statement prints text without moving to next line?
System.out.println()
System.out.print()
System.out.printf()
None of these
What is the output of System.out.print("Hi "); System.out.print("There");?
Hi
There
Hi There
HiThere
Which of these is NOT a valid way to display output?
System.out.print("Hello")
System.out.println("Hello")
System.print("Hello")
System.out.printf("Hello")
Which of these is a correct way to declare an integer variable?
int x;
x int;
integer x;
var int x;
Which data type is used to store decimal numbers?
int
float
String
char
Which data type is used for very precise decimal values?
int
float
double
char
Which of these is used to store text?
char
String
int
double
What is the size of int in Java?
2 bytes
4 bytes
8 bytes
1 byte
Which of these is a valid variable name?
1number
number_1
int
double
What is the default value of int if not initialized?
0
null
0.0
garbage value
Which keyword is used to declare a floating-point variable?
int
double
float
char
How do you assign value 10 to an integer variable x?
int x = 10;
x = int 10;
x := 10;
int x := 10;
Which is correct declaration of a String variable?
string name = "John";
String name = "John";
str name = "John";
char name = "John";
How do you print variable x having value 5?
System.out.println(x);
print(x);
System.out.print("x");
printf(x);
How do you print text and variable together?
System.out.println("Value: " + x);
System.out.println("Value: ", x);
System.out.print("Value:" x);
print("Value:" + x);
What is the output of: int x=10; System.out.print(x);?
x
10
Error
nothing
What does + do in a print statement?
Adds numbers only
Joins strings only
Works for both addition and joining
None
How do you display int a=5 and int b=10 as 5 10?
System.out.println(a b);
System.out.println(a + b);
System.out.println(a + " " + b);
System.out.println("a b");
Which package must you import to use Scanner?
java.util.Scanner
java.io.Scanner
java.Scanner
scanner.util
How do you create Scanner object?
Scanner sc = new Scanner();
Scanner sc = Scanner();
Scanner sc = new Scanner(System.in);
Scanner sc = new scanner(System.in);
Which method reads an integer input?
nextInt()
nextInteger()
getInt()
readInt()
Which method reads a whole line of text?
next()
nextLine()
readLine()
nextText()
Which method reads a single word?
next()
nextLine()
nextWord()
readWord()
What does System.in represent?
Output stream
Input stream
Error stream
File stream
Which of these is correct?
int x = sc.nextInt();
String s = sc.next();
float f = sc.nextFloat();
All of the above
If you forget to import Scanner, what happens?
No error
Compile-time error
Run-time error
Warning only
What is the correct sequence to read an integer?
Declare variable → Create Scanner → Read input
Read input → Create Scanner → Declare variable
Read input → Declare variable → Create Scanner
None of the above
What happens if you enter a letter when nextInt() is called?
Reads as 0
Runtime error
Reads as string
Skips
Which is the correct syntax of if statement?
if {condition} {statements}
if (condition) {statements}
if condition {statements}
if [condition] {statements}
What is the output of: if(5>3) System.out.print("Yes");?
Yes
No
Error
Nothing
What does else mean?
Executes if condition is true
Executes if condition is false
Executes always
Does nothing
What is the output: int x=10; if(x<5) System.out.print("Small"); else System.out.print("Big");?
Small
Big
Error
Nothing
Can if exist without else?
Yes
No
Can else exist without if?
Yes
No
What is the output: int x=5; if(x==5) System.out.print("Equal"); else System.out.print("Not Equal");?
Equal
Not Equal
Which operator is used for equality check?
=
==
!=
equals()
Which operator is used for NOT equal?
=
==
!=
<>
How many if…else if…else can be chained?
Only one
Only two
Any number
None
Which of these is true?
if-else is used for decisions
if-else is used for loops
if-else is used for output
if-else is used for input
Which is valid?
if(x=5)
if(x==5)
if x=5
if x==5
What happens if no condition is true and no else is given?
Error
Nothing happens
Executes next statement
Both b and c
Which is correct nested if?
if(x>0) if(x<10) System.out.print(x);
if(x>0) then if(x<10)
if(x>0 and x<10)
if(x>0: if(x<10))
What is the output: int x=7; if(x>5) if(x<10) System.out.print("OK");?
OK
Error
Nothing
What is the output: if(false) System.out.print("Yes"); else System.out.print("No");?
Yes
No
Which of these can replace if-else in some cases?
while
switch
for
do-while
What is evaluated in an if condition?
integer
float
boolean
string
Which of these is invalid?
if(true)
if(false)
if(0)
if(x>0)
What is the output: if(5<3) System.out.print("Yes"); else System.out.print("No");?
Yes
No
