AlgoMaster Logo

Basic Syntax - Quiz

Last Updated: January 3, 2026

Basic Syntax Exercises

31 quizzes

1
Code Completion

Complete the statement so the program reads an integer from standard input using an existing Scanner named scanner

java
1
int age = scanner.();

Click an option to fill the blank:

2
Code Completion

Complete the statement so the variable userName refers to a new String object from an existing text variable

java
1
String userName = new (text);

Click an option to fill the blank:

3
Code Completion

Complete the statement so result holds the remainder when total is divided by 2

java
1
int result = total 2;

Click an option to fill the blank:

4
Multiple Choice

Which declaration defines a local variable that can store a person's age in whole years?

5
Multiple Choice

Which primitive type is best suited to represent a true/false flag?

6
Multiple Choice

Which statement about reference types in Java is correct?

7
Multiple Choice

What is the result type of the expression 5 / 2 in Java when both operands are int?

8
Multiple Choice

Which casting operation requires explicit casting syntax?

9
Multiple Choice

Which line correctly creates a Scanner to read from standard input?

10
Multiple Choice

How is a single-line comment written in Java?

11
Multiple Choice

Which variable name follows standard Java naming conventions for a boolean flag?

12
Multiple Choice

Which declaration defines a constant in Java that represents the maximum number of users?

13
Multiple Choice

Which primitive type is best for representing the mathematical constant pi with decimals?

14
Multiple Choice

Which statement about local variables is true?

15
Sequencing

Order the steps to read a user's name from the console and print a greeting using Scanner

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to declare, initialize, and use an instance variable in a class

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code?

1int a = 7;
2int b = 3;
3int result = a / b * b;
4System.out.println(result);
18
Output Prediction

What is the output of this code?

1byte x = 100;
2byte y = 27;
3int sum = x + y;
4System.out.println(sum);
19
Output Prediction

What is the output of this code?

1char letter = 'A';
2int code = letter + 1;
3System.out.println(code);
20
Output Prediction

What is the output of this code?

1double price = 19.99;
2int rounded = (int) price;
3System.out.println(rounded);
21
Output Prediction

What is the output of this code?

1String firstName = "Ada";
2String lastName = "Lovelace";
3System.out.println(firstName + " " + lastName.length());
22
Bug Spotting

Find the bug in this code that reads an age and prints it

Click on the line(s) that contain the bug.

java
1
import java.util.Scanner;
2
 
3
public class AgeReader {
4
    public static void main(String[] args) {
5
        Scanner scanner = new Scanner(System.in);
6
        int age;
7
        System.out.print("Enter age: ");
8
        age = scanner.nextLine();
9
        System.out.println("Age: " + age);
10
        scanner.close();
11
    }
12
}
23
Bug Spotting

Find the bug in this code that calculates a discount using doubles

Click on the line(s) that contain the bug.

java
1
public class DiscountCalculator {
2
    public static void main(String[] args) {
3
        double price = 50.0;
4
        double discountRate = 0.1;
5
        double finalPrice = price - price * (int) discountRate;
6
        System.out.println(finalPrice);
7
    }
8
}
24
Matching

Match each operator with its category

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each naming convention with the corresponding Java element

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to declare a constant for maximum attempts and read the first attempt count from the user

java
1
import java.util.Scanner;
2
3
public class LoginAttempts {
4
public static void main(String[] args) {
5
int MAX_ATTEMPTS = 3;
6
Scanner scanner = new Scanner(System.in);
7
System.out.print("Enter attempts used: ");
8
int used = scanner.();
9
System.out.println("Remaining: " + (MAX_ATTEMPTS - used));
10
scanner.close();
11
}
12
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to store a user's initial and age using appropriate primitive types

java
1
public class UserInfo {
2
public static void main(String[] args) {
3
initial = 'J';
4
age = 25;
5
System.out.println(initial + " is " + age + " years old");
6
}
7
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to safely narrow a long value to an int and print it

java
1
public class CastExample {
2
public static void main(String[] args) {
3
long big = 1000L;
4
int smaller = big;
5
System.out.println();
6
}
7
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to use a boolean variable that follows naming conventions and a comment explaining its purpose

java
1
public class FeatureToggle {
2
public static void main(String[] args) {
3
// indicates if the beta feature is enabled
4
boolean = true;
5
if () {
6
System.out.println("Beta feature is ON");
7
}
8
}
9
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that defines a static variable shared by all instances

Click on the line to select.

java
1
public class Counter {
2
    int instanceCount = 0;
3
    static int totalCount = 0;
4
 
5
    public void increment() {
6
        instanceCount++;
7
        totalCount++;
8
    }
9
}
31
Hotspot Selection

Click the line that will cause a NullPointerException at runtime

Click on the line to select.

java
1
public class ReferenceTest {
2
    public static void main(String[] args) {
3
        String message = null;
4
        System.out.println(message.length());
5
    }
6
}