WorksheetsY12 Java syntax recap
Total questions: 27
Worksheet time: 19mins
How are blocks of code demarcated in Java?
{ }
[ ]
( )
using indentation
Give the line of code to display "Hello World" on the console.
(a)
What is the operator used to check if two values are equal?
=
==
!=
=>
Which of these statements correctly tests whether x is less than 4?
if (x<4) {
if(x<=4) {
if [x<4] {
if [x<=4] {
Which of these variables uses the preferred "camel case" style?
myObject
array_index
TotalLength
is_Answer_Correct
Which datatype is used to represent floating-point numbers?
(a)
Which datatype is used to represent text, eg "hello"?
(a)
Which of these is the correct array declaration?
double lengths = [23.4, 10.1, 0.5];
double[] lengths = [23.4, 10.1, 0.5];
[double] lengths = {23.4, 10.1, 0.5}
double[] lengths = {23.4, 10.1, 0.5};
Which of these is NOT a valid Java type?
int
integer
Integer
char
What is the value of i, when the loop terminates?
(a)
Complete the for loop, so that it prints the numbers 1 to 10.
(a)
What is the purpose of the following statement?
Scanner input = new Scanner(System.in);
It declares a variable called Scanner
It instantiates a keyboard input object
It accepts keyboard input and stores it in a variable
It waits for the user to press a key
What is the difference between a function and a method in Java?
functions may not return a value
methods have a void return type
functions are used within a class
methods are usually static
Why does this code produce an error?
the method has no return value
the return type should be String
the method should be called using sayHi();
the method should not be static
What should replace the ??? so that the element that contains 8 is printed?
(a)
What is the correct way to obtain the length of the simple array numbers?
len(numbers)
numbers.length
numbers.length()
numbers.size()
What is the correct way to get the number of columns in the 2D array grid?
grid.length
grid[0].length
grid[].length
length(grid[])
How would you declare an ArrayList of integers?
ArrayList<Integer> myArray = new ArrayList<>();
ArrayList<> myArray = new ArrayList<Integer>();
ArrayList<int> myArray = new ArrayList<>();
ArrayList<> myArray = new ArrayList<int>();
What is the correct way to get the number of elements in the ArrayList myArray?
myArray.length
myArray.length()
myArray.size
myArray.size()
Which of these is an advantage of ArrayLists over simple arrays?
ArrayLists use less memory per element
ArrayLists can be dynamically resized
ArrayLists can work with both simple types and objects
ArrayLists are faster
Which of these is NOT a true statement about classes?
A class is a set of data and the methods that operate on this data
the int datatype is an example of a class
Class names in Java always start with a capital letter
All code in a Java program must be contained within a class
The process of creating a new object of a class is called...
(a)
On average, how many elements will need to be checked, when performing a linear search on an unsorted array of n elements?
n
n/2
2n
log(n)
Why does sorting an array improve the efficiency of a linear search?
You can jump straight to the target element
it doesn't - unsorted lists are quicker to search
the search can terminate as soon as the correct position for the target element has been reached
arrays are more efficient when sorted
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)
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)
public static ??? main(String[] args) {
what should replace the ???
(a)
