WorksheetsJava Programming Quiz - unit 1b
Total questions: 20
Worksheet time: 10mins
Where should the import java.util.Scanner; statement be placed in a Java program?
Inside the main method
At the beginning of the program before the class declaration
At the end of the program
Anywhere in the program
What will the following code output? String text = "Hello, Java!"; int index = text.indexOf("Java"); System.out.println(index);
7
6
5
8
What is the value of y after the following statements? int y = 4; y *= 2;
2
4
6
8
What does \t represent in a string?
A tab space
A new line
A backspace
A space
What will System.out.println("Java".charAt(2)); output?
J
a
v
Throws an error
What will the following code output? String text = "Hello, World!"; char ch = text.substring(7, 12).charAt(2); System.out.println(ch);
W
r
l
o
What method is used to read a double value using a Scanner object?
nextDouble()
getDouble()
readDouble()
inputDouble()
What will the following code output? String a = "Hello"; String b = "World"; System.out.println(a + " " + b);
Hello World
HelloWorld
Hello
Throws an error
Which of the following declares a character variable correctly?
char ch = 'A';
char ch = "A";
char ch = A;
char ch <- 'A';
What will System.out.println("Java".length()); output?
3
4
5
Throws an error
What is the value of x after the following code? int x = 9; x /= 3;
1
3
6
9
What does the following code output? String str = "abcdef"; String result = str.substring(str.indexOf('c'), str.indexOf('e')); System.out.println(result);
cd
ce
cde
Throws an Error
What does str.lastIndexOf('a') return for the string str = "banana"?
1
3
5
Throws an error
Which of the following correctly declares a string variable in Java?
String name = "Alice";
string name = "Alice";
String name == "Alice";
String name: "Alice";
What will "hello".equals("Hello") return?
true
false
Throws an error
null
What will the following code output? String str = "Programming"; String sub = str.substring(0, 5); System.out.println(sub);
Progr
rammi
gramm
Programming
What will the following code output? String str = "hello"; System.out.println(str.toUpperCase());
HELLO
hello
Hello
Throws an error
If you create a Scanner object as follows,
Scanner sc = new Scanner(System.in);
How can you read an integer input?
int num = sc.nextInt();
int num = sc.readInt();
int num = sc.getInt();
int num = sc.inputInt();
What will the value of x be after the following statements? int x = 5; x = x + 3;
3
5
8
15
What do the following lines of code do?
Scanner sc = new Scanner(System.in); String name = sc.nextLine();
Read a single character
Read an integer
Read a whole line of input
Throws an error
