WorksheetsQuiz 4: Java Syntax
Total questions: 25
Worksheet time: 25mins
Insert the missing part of the code below to output "Hello World".
public class MyClass {
public static void main(String[] args) {
______.___.________("Hello World");
}
}
(a)
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)
Create a variable named carName and assign the value Volvo to it.
____________ _________ = ___________;
(a)
Create a variable named maxSpeed and assign the value 120 to it.
____ ___________ = _____;
(a)
Display the sum of 5 + 10, using two variables: x and y.
----- ----- = ---- ;
int y = 10;
System.out.println(x + y);
(a)
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)
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)
Add the correct data type for the following variables:
---- myNum = 9;
--myFloatNum = 8.99f;
--myLetter = 'A';
--myBool = false;
--myText = "Hello World";
(a)
byte, short, int, long, float, double, boolean and char are called:
(a) data types.
Type casting - convert the following double type (myDouble) to an int type:
double myDouble = 9.78d;
int myInt = (a) myDouble;
Multiply 10 with 5, and print the result.
System.out.println(10--5);
(a)
Divide 10 by 5, and print the result.
System.out.println(10--5);
(a)
Use the correct operator to increase the value of the variable x by 1.
int x = 10;
(a) x;
Use the addition assignment operator to add the value 5 to the variable x.
int x = 10;
x (a) 5;
Fill in the missing part to create a greeting variable of type String and assign it the value Hello.
______ greeting = ______;
(a)
Use the correct method to print the length of the txt string.
String txt = "Hello";
System.out.println(___._______);
(a)
Convert the value of txt to upper case.
String txt = "Hello";
System.out.println(__._______);
(a)
Use the correct operator to concatenate two strings:
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName (a) lastName);
Use the correct method to concatenate two strings:
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName. (a) (lastName));
Return the index (position) of the first occurrence of "e" in the following string:
String txt = "Hello Everybody";
System.out.println(txt._____(_____));
(a)
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) ;
}
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)
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)
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));
}
Print i as long as i is less than 6.
int i = 1;
____ (i < 6) {
System.out.println(i);
____;
}
(a)
