AI InterviewSearch…Search problems, topics, or pages…Ctrl KGet started
Skip to content

02 Types & operators / Checkpoint

Checkpoint — Types & operators

25 min · +25 XP · 3 exercises · 15 questions

Exercises0/3
Quiz · 70% to clear15 q

This checkpoint covers the eight primitives, variables and constants, conversions and promotion, integer arithmetic and overflow, floating point, operators and precedence, and the wrapper classes.

How it works. Fifteen questions and two programs; 70% on the questions and both programs accepted clears the module. Retake as often as you like — the best score counts.

Before you start, make sure these are automatic:

  • The ranges of int and long, and what Integer.MAX_VALUE + 1 gives.
  • Why byte b = a + b fails and b += a compiles.
  • What (int) 3.99, Math.round(3.5) and -7 % 3 evaluate to.
  • Why 0.1 + 0.2 != 0.3, and what to use for money.
  • The difference between && and &, >> and >>>.
  • When Integer == Integer is true and when it is not.

The programs exercise exactly the traps above: one is about doing arithmetic in the right type, the other about reading and formatting decimals without losing cents.

Common questions

What does Integer.MAX_VALUE + 1 give in Java?

Integer.MIN_VALUE, which is -2,147,483,648. int arithmetic wraps around silently in two's complement, with no exception; use Math.addExact to throw on overflow, or compute in long.

What do (int) 3.99, Math.round(3.5) and -7 % 3 evaluate to?

(int) 3.99 is 3, because a cast truncates toward zero; Math.round(3.5) is 4, returned as a long; -7 % 3 is -1, because % takes the sign of the dividend. Math.floorMod(-7, 3) gives 2.

Why does byte b = a + b fail but b += a compile?

Arithmetic on byte values is done in int, so a + b is an int that needs a cast to go back into a byte. A compound assignment such as b += a contains that cast implicitly: it means b = (byte) (b + a).

Java Types and Operators Quiz: Casting and Overflow Practice

Fifteen questions and two programs on Java primitive types, casting, integer overflow, floating-point precision, money, operators and wrapper classes.

  • Course: Java study plan
  • Module: Types & operators
  • Kind: Checkpoint — cleared at 70%
  • Reading time: 25 min
  • Runtime: OpenJDK 18

Checkpoint — Types & operators is the checkpoint that closes the Types & operators module: a graded quiz and whole-program exercises, passed at 70%.

Instructions

This checkpoint covers the eight primitives, variables and constants, conversions and promotion, integer arithmetic and overflow, floating point, operators and precedence, and the wrapper classes.

How it works. Fifteen questions and two programs; 70% on the questions and both programs accepted clears the module. Retake as often as you like — the best score counts.

Before you start, make sure these are automatic:

  • The ranges of int and long, and what Integer.MAX_VALUE + 1 gives.
  • Why byte b = a + b fails and b += a compiles.
  • What (int) 3.99, Math.round(3.5) and -7 % 3 evaluate to.
  • Why 0.1 + 0.2 != 0.3, and what to use for money.
  • The difference between && and &, >> and >>>.
  • When Integer == Integer is true and when it is not.

The programs exercise exactly the traps above: one is about doing arithmetic in the right type, the other about reading and formatting decimals without losing cents.

Common questions

What does Integer.MAX_VALUE + 1 give in Java?

Integer.MIN_VALUE, which is -2,147,483,648. int arithmetic wraps around silently in two's complement, with no exception; use Math.addExact to throw on overflow, or compute in long.

What do (int) 3.99, Math.round(3.5) and -7 % 3 evaluate to?

(int) 3.99 is 3, because a cast truncates toward zero; Math.round(3.5) is 4, returned as a long; -7 % 3 is -1, because % takes the sign of the dividend. Math.floorMod(-7, 3) gives 2.

Why does byte b = a + b fail but b += a compile?

Arithmetic on byte values is done in int, so a + b is an int that needs a cast to go back into a byte. A compound assignment such as b += a contains that cast implicitly: it means b = (byte) (b + a).

Exercises

Statistics without overflow

Read an integer n (1 ≤ n ≤ 100 000), then n integers each within the int range. Print four lines: sum=<exact sum>, min=<smallest>, max=<largest>, and avg=<mean with exactly 2 decimals>.

The sum of 100 000 values near Integer.MAX_VALUE does not fit in an int. Choose your types accordingly, and make sure the average is computed in floating point, not truncated.

Example: 3 then 2147483647 2147483647 1 →

sum=4294967295
min=1
max=2147483647
avg=1431655765.00

Cents, exactly

A shop stores prices as strings with exactly two decimals. Read an integer n, then n lines each holding a price and a quantity (19.99 3). Work entirely in cents using long — convert each price by splitting on the dot, never by parsing it as a double — and print:

  1. total=<amount> formatted as whole.cc (two-digit cents),
  2. discount=<amount> — 15% of the total, in cents, rounded half up to the nearest cent, in the same format,
  3. due=<total - discount> in the same format.

Example: 2 then 19.99 3 and 0.05 1 →

total=60.02
discount=9.00
due=51.02

(9.003 rounds to 9.00; 60.02 × 0.15 = 9.003.)

Binary clock

Read a number of seconds since midnight (0 ≤ s < 86400). Print the time as HH:MM:SS with %02d, then each component as a six-bit binary string (pad with zeros: String.format("%6s", Integer.toBinaryString(v)).replace(' ', '0')), then bits set: <total ones across the three>.

Example: 3661 →

01:01:01
000001 000001 000001
bits set: 3

In this module: Types & operators

← Wrapper classes and autoboxing · Strings are immutable objects →