Loading...
Loading...
Java interview guide
Syntax, data types, operators, control flow, methods, arrays and strings. Every answer is short enough to say aloud, and every question links to the lesson that teaches it.
61 questionsLast updated
You write and compile your program one time. The compiler makes bytecode, and any computer with a compatible JVM can run that same bytecode. So one program works on Windows, macOS and Linux.
Lesson: What is Java?Java has been used since 1995, so it is stable and well tested. The same program runs on many kinds of computers. That is why many banks, shopping sites and Android apps use it.
Lesson: What is Java?Yes. To only run a program that is already compiled, a Java runtime is enough. To compile your own code, you need the JDK, because it has javac.
No. A modern JDK already includes the runtime parts. For new Java versions, Oracle offers only the JDK as a download.
Lesson: JDK, JRE and JVMThe main method is where your program starts in the traditional structure this course uses. When you type java Hello, the JVM finds public static void main(String[] args) in the Hello class and runs the code inside it.
A semicolon ; marks the end of a statement, like a full stop ends a sentence. It tells the compiler where one instruction stops. If you forget it, you see the error ';' expected.
Declaring creates the variable: int marks;. Initializing creates it and gives it a first value in the same line: int marks = 85;.
camelCase is the naming style Java programmers use for variables. The first word starts with a small letter and every new word starts with a capital letter, like totalMarks.
A data type tells Java what kind of value a variable can hold, such as a whole number, a decimal number, a character, true/false or text.
Lesson: Data types in JavaString variable store?A String is a class, so it is a reference type. The variable stores a reference, a link to where the text is kept in memory, while an int variable stores the number itself.
int and long?Both hold whole numbers. An int uses 4 bytes and goes up to about 2.1 billion. A long uses 8 bytes and holds much bigger numbers.
L and not a small l?Both work, but a small l looks almost the same as the digit 1. A capital L is easy to read and avoids mistakes.
int can hold?2147483647, which is about 2.1 billion. For anything bigger, use long.
f after a float value?A decimal literal like 4.3 is a double. Putting it in a smaller float may lose detail, so Java asks you to write 4.3f to show you mean a float.
double hold a whole number?Yes. double cash = 500; works, and Java stores it as 500.0. That is why it prints with .0.
boolean in Java?No. Some languages allow it, but Java does not. A boolean accepts only true or false.
'7' the same as the number 7?No. '7' is a character, stored as its Unicode number 55, while 7 is an int. Use a char for a digit you only show, and a number type for maths.
"A" and 'A'?"A" in double quotes is a String. 'A' in single quotes is a char, which holds exactly one character.
toUpperCase() change the original String?No. It gives back a new String in capitals. The original variable keeps its text, so you store the result if you need it: String loud = name.toUpperCase();.
double to int?The decimal part would be lost. Java wants you to show that you know this, so it asks for a cast like (int) price.
7 / 2 give 3 in Java?Both 7 and 2 are int, so Java gives an int answer and drops the .5. Write (double) 7 / 2 or 7.0 / 2 to get 3.5.
long to int?The number does not fit in an int, so the cast gives a wrong number. For example, (int) 3000000000L gives -1294967296, and Java shows no error. A cast never checks the range, so check it yourself or keep the bigger type.
final variable get its value on a later line?Yes, once. final int MAX_MARKS; followed by MAX_MARKS = 100; compiles. A second assignment is a compile error, because a final variable gets exactly one value.
You are using a local variable (one created inside a method like main) before giving it a value. Java requires a value first, so give the variable a value when you create it.
Use a bigger type, like long, when a whole number can grow very large. A long holds values up to about 9 billion billion, but it has a limit too.
char grade = "A"; an error?Double quotes make a String, and a String cannot go into a char. Use single quotes: 'A'.
/ and %?/ divides and gives the answer of the division. % gives the remainder, the part left over. 17 / 5 is 3 and 17 % 5 is 2.
-7 / 2 give in Java: -3 or -4?-3. Java drops the decimal part of -3.5, which moves the answer toward zero. It does not round down to -4.
Lesson: Arithmetic operatorsIt is a shortcut like +=, -= or *=. x += 5 changes x using its old value, the same as x = x + 5.
x += 2.5 the same as x = x + 2.5 when x is an int?Not quite. x += 2.5 compiles: Java converts the answer back to int, so the decimal part is dropped. x = x + 2.5 does not compile, because a double cannot go into an int without a cast.
++ work on a double?Yes. If double d = 1.5;, then d++; makes it 2.5. It works on any number type, and on char too.
x = x++; do?It leaves x unchanged. x++ gives the old value and adds 1 to x, but then = stores that old value back in x, so the +1 is lost. Write x++; on its own.
equals() to compare strings?equals() checks if the text is the same. On objects, == checks if both variables refer to the same object in memory, so it can say false for the same text.
> on two boolean values?No. true > false does not compile. Booleans can only be checked with == and !=.
18 <= age <= 60 not work in Java?18 <= age gives a boolean, and Java cannot compare a boolean with 60. Write age >= 18 && age <= 60 instead.
&& and &?Both give the same true/false answer, but && skips the right side when the left side is false, and & always runs both sides. For conditions, use &&: it is faster and safer.
Because it has three parts: the condition, the value if true and the value if false.
Lesson: The ternary operator ?:Only if Java can combine them. int and double combine into a double, but text and a number cannot go into one String variable. The simple rule: use the same type in both results.
"Total: " + 10 + 20 print Total: 1020?Java works left to right. Text comes first, so 10 is joined as text, and then 20 is joined too. Parentheses around 10 + 20 fix it.
% run before +?Yes. % is on the same level as * and /, so 10 + 7 % 3 is 10 + 1, which is 11.
5 / 2 give 2, and how do you get 2.5?Both numbers are int, so the decimal part is dropped. Make one of them a double: 5 / 2.0 gives 2.5.
equals()?equals() checks the text. == checks if both are the same object, so it can say false for the same text.
Put \n inside the String, for example System.out.println("Line 1\nLine 2");. Everything after \n starts on a new line.
println message?Once Java has joined text, every later + also joins text, from left to right. Brackets make Java add the numbers first, so "Total: " + (10 + 20) prints Total: 30.
println and printf?println prints one value as it is and ends the line. printf prints a text with placeholders filled in a chosen shape, and it does not end the line unless you add %n.
printf and String.format?Both use the same placeholders. printf prints the result, while String.format returns it as a String for you to store, join or print later.
Scanner in Java?Scanner is a ready-made class in the java.util package. It reads input, such as what the user types on the keyboard, and turns it into values like int or String.
The program waits silently at each read. A prompt tells the user what to type, so the program does not look stuck.
Lesson: Reading input with Scannersc.close()?It tells Java we have finished with the Scanner, so it can let go of what the Scanner was using. After that the Scanner cannot read any more.
Lesson: Reading input with ScannerYes. Scanner splits the input at spaces, so each nextInt() or nextDouble() reads the next number. Typing them on separate lines works too.
nextDouble() for a price even if most prices are whole?nextDouble() accepts both 102 and 102.50. With nextInt(), the first price with a decimal point would stop the program.
nextLine() skipped after nextInt()?nextInt() reads the number but leaves the line end from the Enter key in the input. The next nextLine() finds that line end straight away and returns an empty String, so it never waits.
next() also return an empty String after nextInt()?No. next() skips spaces and line ends before a word, so it waits for the user. Only nextLine() is affected.
The first line names the exception, such as InputMismatchException. Then I look for the at line that names my own file, for example AgeCheck.java:7, which gives the line that failed.
It is usually waiting at a read like nextInt(). If there is no prompt, or the IDE's console panel is not focused, the user does not know or cannot see that input is expected.
nextDouble() and print it with %.2f, instead of using int?Prices can have paise, so an int would either refuse the input or lose the decimals. %.2f then shows exactly two decimals, the way bills are printed.
The leftover line end: nextInt() leaves it, and nextLine() reads it as an empty line. I add sc.nextLine(); after the number, or ask for the name first.
Anything that gives true or false: a comparison like marks >= 35, a boolean variable, or a method that answers yes or no, like equals(). A plain number is not allowed in Java.
It works, but it is extra. isMember is already true or false, so if (isMember) says the same thing more simply.
No. The condition is either true or false, so one of the two blocks always runs.
Use ?: to pick one of two small values, for example to store "Pass" or "Fail". Use if-else when each path runs code, such as printing several lines.
Each lesson has quizzes, practice and more interview questions on the same topic.
Open the first lesson