wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 4: Java Syntax

Total questions: 25

Worksheet time: 25mins

Name
Class
Date
1.


Insert the missing part of the code below to output "Hello World".


public class MyClass {

public static void main(String[] args) {

______.___.________("Hello World");

}

}

(a)  

2.

Comments in Java are written with special characters. Insert the missing parts:


___This is a single-line comment

___This is a multi-line comment___

(a)  

3.

Create a variable named carName and assign the value Volvo to it.


____________ _________ = ___________;

(a)  

4.

Create a variable named maxSpeed and assign the value 120 to it.


____ ___________ = _____;

(a)  

5.

Display the sum of 5 + 10, using two variables: x and y.

----- ----- = ---- ;

int y = 10;

System.out.println(x + y);

(a)  

6.

Create a variable called z, assign x + y to it, and display the result.

int x = 5;

int y = 10;

--- -- = x + y;

System.out.println();

(a)  

7.

Fill in the missing parts to create three variables of the same type, using a comma-separated list:


--- x = 5-- y = 6-- z = 50;

System.out.println(x + y + z);

(a)  

8.

Add the correct data type for the following variables:


---- myNum = 9;

--myFloatNum = 8.99f;

--myLetter = 'A';

--myBool = false;

--myText = "Hello World";

(a)  

9.

byte, short, int, long, float, double, boolean and char are called:


(a)   data types.

10.

Type casting - convert the following double type (myDouble) to an int type:


double myDouble = 9.78d;

int myInt = (a)   myDouble;

11.

Multiply 10 with 5, and print the result.

System.out.println(10--5);

(a)  

12.

Divide 10 by 5, and print the result.

System.out.println(10--5);

(a)  

13.

Use the correct operator to increase the value of the variable x by 1.

int x = 10;

(a)   x;

14.

Use the addition assignment operator to add the value 5 to the variable x.

int x = 10;

x (a)   5;

15.

Fill in the missing part to create a greeting variable of type String and assign it the value Hello.

______ greeting = ______;

(a)  

16.

Use the correct method to print the length of the txt string.

String txt = "Hello";

System.out.println(___._______);

(a)  

17.

Convert the value of txt to upper case.

String txt = "Hello";

System.out.println(__._______);

(a)  

18.

Use the correct operator to concatenate two strings:

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName (a)   lastName);

19.

Use the correct method to concatenate two strings:

String firstName = "John ";

String lastName = "Doe";

System.out.println(firstName. (a)   (lastName));

20.

Return the index (position) of the first occurrence of "e" in the following string:

String txt = "Hello Everybody";

System.out.println(txt._____(_____));

(a)  

21.

Insert the missing part to call myMethod from main.

static void myMethod() {

System.out.println("I just got executed!");

}

public static void main(String[] args) {

(a)   ;

}

22.

Insert the missing part to call myMethod from main two times.

static void myMethod() {

System.out.println("I just got executed!");

}

public static void main(String[] args) {

_______;

_______;

}

(a)  

23.

Add a fname parameter of type String to myMethod, and output "John Doe".

static void myMethod(____ _____ ) {

System.out.println(____+ " Doe");

}

public static void main(String[] args) {

myMethod("John");

}

(a)  

24.

Insert the missing part to print the number 8 in main, by using a specific keyword inside myMethod:

static int myMethod(int x) {

(a)   5 + x;

}

public static void main(String[] args) {

System.out.println(myMethod(3));

}

25.

Print i as long as i is less than 6.

int i = 1;

____ (i < 6) {

System.out.println(i);

____;

}

(a)