wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java syntax

Total questions: 86

Worksheet time: 29mins

Name
Class
Date
1.
What is the correct file extension for a Java program?
a)
.java
b)
.js
c)
.py
d)
.txt
2.
Which keyword is used to declare a variable?
a)
variable
b)
var
c)
int
d)
let
3.
What does the 'public' keyword signify in Java?
a)
It is private
b)
It is global
c)
It is accessible to all
d)
It is immutable
4.
How do you print text to the console in Java?
a)
console.log()
b)
echo()
c)
System.out.println()
d)
PrintText()
5.
What happens if a local int variable is used without initialization?
a)
It is initialized to 0
b)
It is assigned a default value of 1
c)
It results in compilation error
d)
it is assigned a default value of null
6.
What symbol is used to end a Java statement?
a)
.
b)
;
c)
:
d)
#
7.
Which of the following are valid Java data types?
a)
int
b)
float
c)
string
d)
boolean
e)
array
8.
What is the correct syntax for a single-line comment?
a)
//
b)
/* ... */
c)
<!-- ... -->
d)
''
9.
Which of the following are Java keywords?
a)
class
b)
interface
c)
if
d)
switch
e)
constant
10.
What is the output of System.out.println(5 + "5")?
a)
10
b)
55
c)
Error
d)
undefined
11.
How do you declare a constant in Java?
a)
constant
b)
final
c)
const
d)
static
12.
Which of these is NOT a valid variable name in Java?
a)
myVar
b)
$myVar
c)
123myVar
d)
_myVar
13.
Which operator is used for equality comparison in Java?
a)
=
b)
==
c)
===
d)
equals
14.
Which of these is a valid Java method declaration?
a)
void myMethod();
b)
myMethod();
c)
method void()
d)
void method;
15.
What is the result of 10 / 3 in Java?
a)
3
b)
3.3333
c)
4
d)
Error
16.
What is the size of the 'int' data type in Java?
a)
2 bytes
b)
4 bytes
c)
8 bytes
d)
16 bytes
17.
Which method is used to parse a string into an integer?
a)
parseInt()
b)
Integer.parseInt()
c)
toInt()
d)
convertInt()
18.
How do you declare an array in Java?
a)
int[] arr;
b)
array arr;
c)
int arr[];
d)
int arr;
19.
Which keyword is used to import a package in Java?
a)
include
b)
require
c)
import
d)
use
20.
How many primitive data types does Java have?
a)
6
b)
7
c)
8
d)
9
21.
What does the keyword 'static' mean in Java?
a)
Shared by all instances
b)
Instance-specific
c)
Immutable
d)
Private
22.
Which of these is a valid main method signature?
a)
public static void main(String[] args)
b)
static void main(String args)
c)
public void main(String[] args)
d)
public static main()
23.
Which of the following are valid Java keywords?
a)
class
b)
int
c)
public
d)
integer
e)
static
24.
What are the valid access modifiers in Java?
a)
private
b)
public
c)
protected
d)
global
e)
default
25.
Which of these are valid variable names in Java?
a)
myVar
b)
$value
c)
_temp
d)
123Var
e)
userName
26.
Which of these are primitive data types in Java?
a)
int
b)
char
c)
boolean
d)
String
e)
double
27.
Which symbols are used in Java for comments?
a)
//
b)
/* */
c)
<!-- -->
d)
###
e)
/** */
28.
Which of the following are valid literals in Java?
a)
123
b)
"Hello"
c)
A'
d)
True
e)
0.99
29.
What are the valid data types for numeric values in Java?
a)
int
b)
double
c)
float
d)
boolean
e)
short
30.
What are the valid escape sequences in Java?
a)
\n
b)
\t
c)
\r
d)
\b
e)
#
31.
Which of the following can be used for a method's return type?
a)
void
b)
int
c)
boolean
d)
double
e)
method
32.
What are valid loop control statements in Java?
a)
break
b)
continue
c)
exit
d)
return
e)
stop
33.
Which of the following are valid operators in Java?
a)
=+
b)
-
c)
==
d)
!==
e)
*=
34.
What can be included in a method declaration?
a)
Method name
b)
Return type
c)
Parameters
d)
Access modifier
e)
Constant declaration
35.
What are valid loop constructs in Java?
a)
for
b)
while
c)
do-while
d)
foreach
e)
loop
36.
What are valid integer data types in Java?
a)
byte
b)
short
c)
int
d)
long
e)
float
37.
Which of the following are valid conditional statements in Java?
a)
if
b)
else if
c)
switch
d)
elif
e)
unless
38.
What are valid Java statements for declaring variables?
a)
int x = 10;
b)
double pi = 3.14;
c)
boolean isActive;
d)
String name = "John";
e)
var a = 10;
39.
What are valid boolean expressions in Java?
a)
a == b
b)
a && b
c)
a
d)
a = b
e)
!a
40.
Which of the following are valid types for arrays in Java?
a)
int[]
b)
String[]
c)
double[]
d)
char[]
e)
object[]
41.
What are valid identifiers in Java?
a)
myVar
b)
$value
c)
_temp
d)
class
e)
userName
42.
What are valid Java relational operators?
a)
>
b)
>=
c)
<
d)
<=
e)
=
43.
What are valid Java assignment operators?
a)
=
b)
=+=
c)
-=
d)
/=
e)
<<=
44.
Which of these are valid parts of a class in Java?
a)
Fields
b)
Methods
c)
Constructors
d)
Access modifiers
e)
Headers
45.
What are valid ways to declare a String in Java?
a)
String s = "Hello";
b)
String s = new String("Hello");
c)
char[] s = {'H','e','l','l','o'};
d)
String s = 'Hello';
e)
String s = Hello;
46.
What are valid types of casting in Java?
a)
Implicit casting
b)
Explicit casting
c)
Forced casting
d)
Narrowing conversion
e)
Widening conversion
47.
What is the error in the code? int x = "10";
a)
Syntax Error
b)
Type Mismatch
c)
Logical Error
d)
No Error
48.
What is the error in the following code? System.out.print(Hello);
a)
Missing semicolon
b)
Missing quotation marks
c)
Invalid method
d)
No Error
49.
Identify the error in this code: int num; System.out.println(num);
a)
Variable not declared
b)
Variable not initialized
c)
Incorrect variable type
d)
No Error
50.
What is the problem with this loop? for(int i = 0; i <= 5; i--) {}
a)
Infinite Loop
b)
Syntax Error
c)
Logical Error
d)
No Error
51.
What is the error in this code? int arr = {1, 2, 3};
a)
Incorrect array declaration
b)
Missing semicolon
c)
Array index out of bounds
d)
No Error
52.
What is the issue in the following method? public void myMethod {}
a)
Missing parentheses in method declaration
b)
Missing method return type
c)
Missing semicolon
d)
No Error
53.
What is wrong with this code? int x = 5 System.out.println(x);
a)
Syntax Error
b)
Missing semicolon
c)
Logical Error
d)
No Error
54.
What is the error in this code? char c = "a";
a)
Invalid variable type
b)
Incorrect quotation marks
c)
Variable not initialized
d)
No Error
55.
Identify the error in this switch statement: switch(x) { case 1: break; }
a)
Missing default case
b)
Missing curly braces
c)
Missing break keyword
d)
No Error
56.
What is the issue with this variable declaration? float num = 1.5;
a)
Incorrect variable type
b)
Missing f suffix for float
c)
Missing semicolon
d)
No Error
57.
What is the output of the following code? System.out.println("Hello " + 5 * 2);
a)
Hello 7
b)
Hello 10
c)
Hello52
d)
Error
58.
What does this code do? int[] arr = new int[5];
a)
Declares and initializes an array with 5 elements
b)
Declares an array of 5 null values
c)
Declares an array and initializes all values to 0
d)
Declares an empty array
59.
What will happen when this code runs? int x = 5; System.out.println(x/0);
a)
Prints 0
b)
Prints Infinity
c)
Throws an ArithmeticException
d)
Compiles but doesn't run
60.
What is the output of the following code? int x = 10; x += 5; System.out.println(x);
a)
5
b)
10
c)
15
d)
20
61.
Identify the error in the code: int num = "10";
a)
Syntax error
b)
Type mismatch
c)
Runtime error
d)
No error
62.
What is the output of the following loop? for(int i = 0; i < 3; i++) { System.out.print(i + " "); }
a)
0 1
b)
2000 1 2
c)
0 1 2 3
d)
Infinite loop
63.
What will this code print? int x = 10; if(x == 10) { System.out.println("True"); } else { System.out.println("False"); }
a)
True
b)
False
c)
Error
d)
No output
64.
How many times will the following code print "Hi"? int i = 0; while(i < 3) { System.out.println("Hi"); i++; }
a)
0
b)
1
c)
2
d)
3
65.
Which of the following will compile successfully?
a)
System.out.println("Hi");
b)
int x = 10;
c)
int 123x = 10;
d)
char c = 'a';
e)
float num = 1.5;
66.
What is the result of the following code? int x = 5; x = x * 2; x = x - 3; System.out.println(x);
a)
5
b)
7
c)
10
d)
12
67.
Complete the statement to declare an integer variable: ____ x = 10;
a)
int
b)
float
c)
char
d)
String
68.
Fill in the blank to print "Hello, World!": System.out.____("Hello, World!");
a)
print
b)
println
c)
printf
d)
echo
69.
Complete the for loop syntax: for(____; i < 5; i++) { System.out.println(i); }
a)
int i = 0;
b)
i = 0
c)
int i
d)
i == 0
70.
Which of the following completes the array declaration? int[] arr = ____;
a)
[1, 2, 3]
b)
new int[5]
c)
new int[] {1, 2, 3}
d)
new array(3)
71.
What is missing in this method declaration? ____ void myMethod() {}
a)
public
b)
static
c)
private
d)
abstract
72.
Complete the code to read input from the user: Scanner sc = ____ Scanner(System.in);
a)
new
b)
create
c)
initialize
d)
make
73.
Fill in the blank to start the main method: public static ____ main(String[] args) {}
a)
void
b)
int
c)
String
d)
boolean
74.
Which keyword completes this statement? if(x > 0) { ____ y = x + 5; }
a)
int
b)
float
c)
boolean
d)
declare
75.
Complete the loop to print numbers 1 to 10: for(int i = 1; i <= 10; ____)
a)
i++
b)
=++i
c)
i--
d)
--i
76.

What is the output of the following code? System.out.println(3 + 5 / 2);

(a)  

77.

What is the output of this code? System.out.println("Java " + "Programming");

(a)  

78.

Predict the output: int x = 10; System.out.println(x * 2);

(a)  

79.

What will this print? int x = 5; int y = 3; System.out.println(x - y);

(a)  

80.

Enter the output: System.out.println(10 / 4);

(a)  

81.

What is the output of the following code? System.out.println(2 + "2");

(a)  

82.

Enter the output of this loop: for(int i = 1; i <= 3; i++) { System.out.print(i); }

(a)  

83.

What is the output? System.out.println("Value: " + (4 * 5));

(a)  

84.

Predict the output: boolean flag = true; System.out.println(!flag);

(a)  

85.

What is printed by this code? System.out.println(10 % 3);

(a)  

86.

What is the output of the following code? System.out.println(3 + 5 % 2);

(a)