wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Programming Basics Quiz

Total questions: 50

Worksheet time: 40mins

Name
Class
Date
1.

What is the correct keyword to define a class in Java?

a)

Class

b)

public

c)

class

d)

ClassName

2.

In Java, the filename must match the (a)   .

3.

Which of the following statements is true about Java keywords?

a)

They are case-insensitive

b)

They must start with a capital letter

c)

They are written in lowercase

d)

They must be enclosed in double quotes

4.

In Java, curly braces {} are used to:

a)

Initialize variables

b)

Enclose code blocks

c)

Add comments

d)

End a line

5.

Java is a __________ programming language.

a)

case sensitive

b)

case insensitive

6.

What is the correct syntax to define the main method?

a)

static public void Main()

b)

public static void main(String[] args)

c)

public void static main(String args[])

d)

void main public static(String[] args)

7.

Which method advances the cursor to the next line after displaying output?

a)

System.out.print()

b)

System.out.scan()

c)

System.out.println()

d)

System.out.display()

8.

Which method keeps the output cursor on the same line?

a)

System.out.cursor()

b)

System.out.print()

c)

System.out.nextLine()

d)

System.out.println()

9.

In Java, a string must be enclosed within (a)   quotes.

10.

Every Java statement must end with a:

a)

comma(,)

b)

period(.)

c)

colon(:)

d)

semicolon(;)

11.

Identify 2 incorrect ways to print a string:

a)

System.out.print("Hello");

b)

System.out.println("Hello World!");

c)

System.out.println(Hello);

d)

system.out.println("Hello");

12.

Which symbol is used for a single-line comment?

a)

//

b)

/**/

c)

#

d)

--

13.

Which comment syntax allows multiple lines?

a)

\ multi-line \

b)

/* multi-line */

c)

## multi-line ##

d)

// multi-line \\

14.

Choose the correctly commented line:

a)

//This is a comment

b)

\This is a comment\

c)

-- This is a comment --

d)

/** This is a comment /**

15.

Which of the following are valid Java variable declaration?

a)

double distance_4

b)

int 2speed;

c)

String $name;

d)

final 5rate;

16.

Which is the correct syntax to declare multiple int variables?

a)

int width height length;

b)

int width, height, length;

c)

int(width, height, length);

d)

int width: height: length;

17.

When declaring a variable, what must come first?

a)

variable name

b)

value

c)

data type

d)

method

18.

What is the data type for storing text in Java?

a)

text

b)

character

c)

String

d)

word

19.

Which variable name is invalid in Java?

a)

$price

b)

_height

c)

3score

d)

totalAmount

20.

Which statement initializes an integer variable named hours with value 40?

a)

hours = 40 int;

b)

int 40 = hours;

c)

int hours = 40;

d)

int hours 40;

21.

Create a variable named myClass and save the value "Computer Lab":

(a)  

22.

In Java, MyAge and myAge are:

a)

The same

b)

Different

c)

Both invalid

d)

Compiled together

23.

In Java, My-name and my=name are:

a)

The same

b)

Different

c)

Both invalid

d)

Compiled together

24.

Which keyword is used to create a constant in Java?

a)

static

b)

const

c)

final

d)

private

25.

Which of the following is a valid constant declaration?

a)

final double MY_RATE = 0.2;

b)

double FINAL_RATE = 0.2;

c)

constant double RATE;

d)

final RATE = 0.2 double;

26.

Constant variable names are written in:

a)

camelCase

b)

PascalCase

c)

all lowercase

d)

all uppercase

27.

The + operator when used with strings in Java:

a)

adds numbers

b)

concatenates strings

c)

multiplies strings

d)

divides strings

28.

System.out.println("Java" + "Programming"); will output:

a)

JavaProgramming

b)

Java Programming

c)

Java+Programming

d)

Java, Programming

29.

Which expression will correctly combine the value of int age = 20; with text?

a)

System.out.println("Age" + age);

b)

System.out.println("Age", age);

c)

System.out.print("Age" * age);

d)

System.out.print("Age" && age);

30.

Which class must you import to use Scanner in Java?

a)

java.input.Scanner

b)

java.util.Scanner

c)

scanner.util.java

d)

import.scanner.java

31.

What does keyboard.nextInt() do?

a)

Reads a String input

b)

Reads an integer input

c)

Creates an integer variable

d)

Scans the next character

32.

Which method reads a double input using Scanner?

a)

nextDouble()

b)

scanDouble()

c)

readDouble()

d)

doubleInput()

33.

Which method is used to read a full line of text input?

a)

keyboard.readLine()

b)

keyboard.inputString()

c)

keyboard.nextLine()

d)

scanner.getText()

34.

To create a Scanner object, use: Scanner keyboard = new __________;

4 lines
35.

In Java, a block of code must be enclosed in (a)   .

36.

Each Java file can have:

a)

multiple classes, but only one public class

b)

multiple public classes

c)

one class only

d)

no classes

37.

Which keyword is NOT part of the main method header?

a)

public

b)

void

c)

main

d)

private

38.

The main method's name must always be written as __________.

4 lines
39.

When using Scanner, to avoid input mismatch errors, always:

a)

input numbers in quotes

b)

type matching input types

c)

avoid using the import statement

d)

input blank lines

40.

What is the output of: System.out.print("Java"); System.out.println("Rocks");

a)

Java Rocks

b)

JavaRocks

c)

Java Rocks

d)

RocksJava

41.

Choose the correct code to display: Welcome to Java! on the screen.

a)

System.out.println(Welcome to Java!);

b)

System.out.print("Welcome to Java!");

c)

System.print("Welcome to Java!");

d)

System.out.println("Welcome to Java!");

42.

What is the result of: System.out.println(2 + 2);

a)

22

b)

4

c)

"4"

d)

2 + 2

43.

Choose the correct way to create a Scanner object.

a)

Scanner = new Scanner(System.in);

b)

new Scanner(System.in);

c)

Scanner input = new Scanner(System.in);

d)

Scanner input = Scanner(System.in);

44.

What is the correct way to import the Scanner class?

a)

import scanner.util;

b)

import java.util.Scanner;

c)

java.util.Scanner.import;

d)

import java.input.Scanner;

45.

Which of the following will correctly declare and initialize a String variable?

a)

String name = 'John';

b)

String name = "John";

c)

name String = "John";

d)

String = "John" name;

46.

Using + between a string and an integer automatically __________ the integer to a string.

4 lines
47.

What happens if you forget the semicolon after a Java statement?

a)

Nothing

b)

Syntax error

c)

Logical error

d)

Runtime error

48.

Which option is a correct block comment?

a)

// Block comment /* /

b)

/ Block comment */

c)

// Block comment //

d)

*/ Block comment /

49.

In Java, a class name usually starts with:

a)

lowercase letter

b)

uppercase letter

c)

number

d)

symbol

50.

Name one company that mainly code using Java:

4 lines