wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Y12 Java syntax recap

Total questions: 27

Worksheet time: 19mins

Name
Class
Date
1.

How are blocks of code demarcated in Java?

a)

{ }

b)

[ ]

c)

( )

d)

using indentation

2.

Give the line of code to display "Hello World" on the console.

(a)  

3.

What is the operator used to check if two values are equal?

a)

=

b)

==

c)

!=

d)

=>

4.

Which of these statements correctly tests whether x is less than 4?

a)

if (x<4) {

b)

if(x<=4) {

c)

if [x<4] {

d)

if [x<=4] {

5.

Which of these variables uses the preferred "camel case" style?

a)

myObject

b)

array_index

c)

TotalLength

d)

is_Answer_Correct

6.

Which datatype is used to represent floating-point numbers?

(a)  

7.

Which datatype is used to represent text, eg "hello"?

(a)  

8.

Which of these is the correct array declaration?

a)

double lengths = [23.4, 10.1, 0.5];

b)

double[] lengths = [23.4, 10.1, 0.5];

c)

[double] lengths = {23.4, 10.1, 0.5}

d)

double[] lengths = {23.4, 10.1, 0.5};

9.

Which of these is NOT a valid Java type?

a)

int

b)

integer

c)

Integer

d)

char

10.

What is the value of i, when the loop terminates?

(a)  

11.

Complete the for loop, so that it prints the numbers 1 to 10.

(a)  

12.

What is the purpose of the following statement?

Scanner input = new Scanner(System.in);

a)

It declares a variable called Scanner

b)

It instantiates a keyboard input object

c)

It accepts keyboard input and stores it in a variable

d)

It waits for the user to press a key

13.

What is the difference between a function and a method in Java?

a)

functions may not return a value

b)

methods have a void return type

c)

functions are used within a class

d)

methods are usually static

14.

Why does this code produce an error?

a)

the method has no return value

b)

the return type should be String

c)

the method should be called using sayHi();

d)

the method should not be static

15.

What should replace the ??? so that the element that contains 8 is printed?

(a)  

16.

What is the correct way to obtain the length of the simple array numbers?

a)

len(numbers)

b)

numbers.length

c)

numbers.length()

d)

numbers.size()

17.

What is the correct way to get the number of columns in the 2D array grid?

a)

grid.length

b)

grid[0].length

c)

grid[].length

d)

length(grid[])

18.

How would you declare an ArrayList of integers?

a)

ArrayList<Integer> myArray = new ArrayList<>();

b)

ArrayList<> myArray = new ArrayList<Integer>();

c)

ArrayList<int> myArray = new ArrayList<>();

d)

ArrayList<> myArray = new ArrayList<int>();

19.

What is the correct way to get the number of elements in the ArrayList myArray?

a)

myArray.length

b)

myArray.length()

c)

myArray.size

d)

myArray.size()

20.

Which of these is an advantage of ArrayLists over simple arrays?

a)

ArrayLists use less memory per element

b)

ArrayLists can be dynamically resized

c)

ArrayLists can work with both simple types and objects

d)

ArrayLists are faster

21.

Which of these is NOT a true statement about classes?

a)

A class is a set of data and the methods that operate on this data

b)

the int datatype is an example of a class

c)

Class names in Java always start with a capital letter

d)

All code in a Java program must be contained within a class

22.

The process of creating a new object of a class is called...

(a)  

23.

On average, how many elements will need to be checked, when performing a linear search on an unsorted array of n elements?

a)

n

b)

n/2

c)

2n

d)

log(n)

24.

Why does sorting an array improve the efficiency of a linear search?

a)

You can jump straight to the target element

b)

it doesn't - unsorted lists are quicker to search

c)

the search can terminate as soon as the correct position for the target element has been reached

d)

arrays are more efficient when sorted

25.

Given the array

19, 48, 7, 22, 21, 16, 12

What will be the last element in the array after one pass of a bubble sort?

(a)  

26.

Given the array

19, 48, 7, 22, 21, 16, 12

What will be the first element in the array after one pass of a bubble sort?

(a)  

27.

public static ??? main(String[] args) {

what should replace the ???

(a)