Java Language Fundamentals
The base layer of the language: syntax, types, variables, operators, control flow and arrays. Junior interviews live here, and senior ones return to it whenever a puzzle depends on integer overflow, char arithmetic or definite assignment.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- Syntax & Basic Constructs28 questions
- Identifiers4 questions
- Keywords & Reserved Words5 questions
- Comments4 questions
- Statements vs Expressions5 questions
- Blocks & Scope5 questions
- Whitespace & Formatting5 questions
- Data Types26 questions
- Primitive Types6 questions
- Default Values4 questions
- Overflow & Underflow5 questions
- Special Floating-Point Values5 questions
- Literals6 questions
- Variables24 questions
- Local, Instance & Static Variables5 questions
- final Variables & Constants5 questions
- Variable Shadowing5 questions
- var Local Type Inference5 questions
- Definite Assignment4 questions
- Operators40 questions
- Arithmetic Operators5 questions
- Relational & Equality Operators5 questions
- Logical Operators & Short-Circuiting5 questions
- Bitwise & Shift Operators5 questions
- Assignment & Compound Assignment5 questions
- Ternary Conditional Operator5 questions
- instanceof Operator5 questions
- Operator Precedence & Associativity5 questions
- Type Conversion & Casting23 questions
- Primitive Widening & Narrowing5 questions
- Reference Conversion & Casting4 questions
- Autoboxing & Unboxing4 questions
- Wrapper Classes & Number Caching5 questions
- Numeric Promotion Rules5 questions
- Control Flow44 questions
- if / if-else5 questions
- Traditional switch Statement4 questions
- Switch Expressions5 questions
- Pattern Matching in switch5 questions
- for Loop5 questions
- Enhanced for-each Loop5 questions
- while & do-while Loops5 questions
- break, continue & Labels5 questions
- Unreachable Code Detection5 questions
questions
185 · 6 sectionsWhat is a block statement in Java, and what does it have to do with variable scope?
basics
~20 sA block is code wrapped in curly braces { }. It groups statements together. A variable you declare inside a block can only be used inside that block; once the block ends, the variable is gone.
What kinds of comments does Java support, and what is the syntax for each?
basics
~20 sJava has three: single-line starting with //, multi-line wrapped in /* ... /, and Javadoc written as /* ... */ which is a documentation comment the javadoc tool reads. All of them are ignored by the compiler.
What are the standard Java naming conventions for identifiers, and how do conventions differ from the compiler's rules?
basics
~20 sRules are what the compiler enforces; conventions are agreed style. By convention: classes use UpperCamelCase, variables and methods use lowerCamelCase, constants use UPPER_SNAKE_CASE, and packages are all lowercase. Breaking a convention still compiles, but it's bad style.
What characters are legal in a Java identifier, and what are the rules for where they can appear?
basics
~10 sA Java identifier can contain letters, digits, the dollar sign ($), and the underscore (_). It can start with a letter, $, or _, but it cannot start with a digit.
What are keywords (reserved words) in Java, and why can't you use them as identifiers?
basics
~10 sKeywords are words Java reserves for its own grammar (like class, if, return, int). The compiler treats them specially, so you can't use them to name your own variables, methods, or classes.
When you declare an instance field in a Java class without an initializer, what value does it hold, and how does that depend on its type?
basics
~10 sUninitialized fields get a default automatically: 0 for number types, false for boolean, '' (the null character) for char, and null for any object/reference type. You don't have to set them yourself.
Why does this not compile: `int x; System.out.println(x);` inside a method, even though an int field would print 0?
basics
~20 sLocal variables don't get default values. The compiler requires you to assign a value before reading one, so it rejects the code with 'variable x might not have been initialized'. A field would instead default to 0.
What is a literal in Java, and what are the main categories of literals the language supports?
basics
~10 sA literal is a fixed value written directly in source code, like 42, 3.14, 'A', "hello", true, or null. Java has integer, floating-point, character, string, boolean, and the null literal.
What happens when an int computation exceeds Integer.MAX_VALUE in Java, and why?
basics
~10 sJava does not throw an error. The value silently wraps around: adding 1 to the largest int gives the smallest (most negative) int. No warning, no exception.
What are the default values of primitive fields, and when do defaults apply versus when must you initialize?
basics
~10 sUninitialized primitive fields get defaults: 0 for numbers, 0.0 for float/double, '' for char, false for boolean. Local variables get NO default — you must assign before use or the code won't compile.
What is definite assignment in Java, and why does the compiler reject reading a local variable that might be unassigned?
basics
~20 sDefinite assignment is a compile-time check that every local variable is given a value before you read it. If the compiler can find any path where the variable might not be set yet, it refuses to compile and reports a "variable might not have been initialized" error.
What does the final keyword mean when applied to a local variable, and what happens if you try to reassign it?
basics
~10 sfinal means the variable can be assigned a value only once. After that first assignment you cannot change it. Trying to reassign it is a compile error, so the program will not even build.
What is variable shadowing in Java, and can you give a common example?
basics
~20 sShadowing happens when a variable in an inner scope has the same name as one in an outer scope, so the inner one hides the outer one. A classic case is a constructor parameter named the same as a field.
What are the three kinds of variables in Java, and how do they differ in where they live and how long they exist?
basics
~20 sLocal variables live inside a method and disappear when it ends. Instance variables belong to each object and live as long as the object. Static variables belong to the class itself, so there is just one copy shared by everything.
What is `var` in Java, and what does it actually do to a local variable declaration?
basics
~20 svar lets you declare a local variable without writing its type; the compiler figures out the type from the value you assign. The variable still has a fixed type — var just saves you from typing it.
What are the arithmetic operators in Java and what does each one do?
basics
~10 sJava has +, -, *, / and % for add, subtract, multiply, divide, and remainder. There are also unary + and - that keep or flip a number's sign.
What is the simple assignment operator (=) in Java, and what does an assignment expression evaluate to?
basics
~20 sThe = operator stores the value on its right into the variable on its left. An assignment is also an expression whose result is the value that was assigned, so you can write a = b = 5 and both become 5.
What do Java's bitwise operators &, |, ^, and ~ do, and how do they differ from the logical operators && and ||?
basics
~20 s&, |, ^ compare two numbers bit by bit (AND, OR, XOR); ~ flips every bit. They work on whole numbers. && and || work on true/false, only look at the second value if needed, and give back true or false.
What does the instanceof operator do in Java, and what type of value does it produce?
basics
~10 sinstanceof checks whether an object is an instance of a given type (class or interface). It returns a boolean: true if the object's type matches that type or a subtype, otherwise false.
What are Java's logical operators &&, ||, and !, and what does each do?
basics
~20 s&& is logical AND: true only if both sides are true. || is logical OR: true if at least one side is true. ! is NOT: it flips a boolean, turning true into false and false into true.
What are autoboxing and unboxing in Java, and when does the compiler insert them?
basics
~20 sAutoboxing is the automatic conversion of a primitive (like int) to its wrapper object (Integer). Unboxing is the reverse. The compiler inserts these conversions for you when a primitive is used where an object is expected, or vice versa.
What is binary numeric promotion in Java, and what type results when you add a byte and a short?
basics
~10 sWhen you do arithmetic on two numbers, Java converts both to a common type before computing. Anything smaller than int (byte, short, char) becomes int first. So byte + short gives an int.
What is a widening primitive conversion in Java, and why does it happen automatically?
basics
~20 sWidening turns a smaller numeric type into a bigger one, like int to long or int to double. Java does it automatically because the bigger type can always hold the value, so no data is lost.
What is the difference between widening and narrowing reference conversion in Java (upcasting vs downcasting)?
basics
~10 sUpcasting treats an object as one of its parent types and happens automatically. Downcasting treats it as a more specific child type and must be written explicitly with a cast like (Dog) animal.
What are wrapper classes in Java, and why do they exist?
basics
~20 sWrapper classes like Integer, Long, Double, Boolean, and Character are object versions of the eight primitive types. They exist because generics and collections only hold objects, not primitives, and they add helper methods and constants.
What do the break and continue statements do inside a loop in Java?
basics
~10 sbreak stops the loop entirely and jumps to the code right after it. continue skips the rest of the current iteration and goes straight to the next one (re-checking the loop condition).
What is the enhanced for-each loop in Java and what is its syntax?
basics
~10 sIt is a shorter loop for going through every element of an array or collection without a counter. You write 'for (Type item : collection)' and 'item' takes each value in turn.
What are the three parts of a Java for loop, and in what order do they execute?
basics
~10 sA for loop has three parts in parentheses, separated by semicolons: initialization, condition, and update. Init runs once first; then each pass checks the condition, runs the body, then runs the update.
What is the if statement in Java and how does the optional else clause work?
basics
~10 sAn if statement runs a block of code only when its condition is true. An optional else block runs when the condition is false. The condition must be a boolean.
How do else-if chains work in Java, and how does ordering affect which branch runs?
basics
~10 sAn else-if chain tests conditions in order from top to bottom. The first condition that is true runs its block, and the rest are skipped. An optional final else catches everything else.