Java Fundamentals Interview Questions & Answers
Java Syntax & Structure
Q1. What is the basic structure of a Java program?
A Java program must contain a class and a main() method (entry point).
Example:
class Demo {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
Q2. Why must Java code be inside a class?
Because Java is object-oriented, and everything in Java belongs to a class.
Q3. What is the entry point of a Java program?
public static void main(String[] args)
Q4. Why does main() method have String[] args?
It stores command-line arguments.
Variables
Q5. What is a variable?
A variable is a name given to a memory location where data is stored.
Q6. What are the types of variables in Java?
- Local Variable – Inside method
- Instance Variable – Inside class, outside method
- Static Variable – Declared using
statickeyword
Example:
class Test {
int age = 25; // instance variable
static String name = "Java"; // static variable
void show() {
int x = 10; // local variable
System.out.println(x);
}
}
Q7. Why should variables be initialized?
Because Java does not automatically initialize local variables.
Data Types, Type Casting & Type Promotion
Q8. What are data types in Java?
Two categories:
Primitive (8 types)
- byte, short, int, long
- float, double
- boolean
- char
Non-Primitive
- String
- Arrays
- Classes
- Interfaces
Q9. What is type casting?
Converting one data type to another.
Two types:
1. Implicit / Widening (smaller → larger)
int a = 10;
double b = a; // auto conversion
2. Explicit / Narrowing (larger → smaller)
double x = 10.5;
int y = (int) x; // explicit cast
Q10. What is type promotion in Java?
In expressions, smaller types automatically convert to larger types.
Example:
byte a = 10;
byte b = 20;
int c = a + b; // byte promoted to int
Operators (Arithmetic, Relational, Logical, Bitwise, Ternary)
Q11. What are arithmetic operators?
Perform mathematical calculations like addition, subtraction, multiplication, division, modulus, increment, and decrement.
+ - * / % ++ --
Example:
int a = 10, b = 3;
System.out.println(a % b); // 1
Q12. What are relational operators?
Compare two values and return true or false. (== != > < >= <=)
Example:
System.out.println(10 > 5); // true
Q13. What are logical operators?
Combine boolean expressions. && || !
Example:
boolean x = true, y = false;
System.out.println(x && y); // false
Q14. What are bitwise operators?
Perform operations on individual bits of integers. & | ^ ~ << >> >>>
Example:
int a = 5; // 0101
int b = 3; // 0011
System.out.println(a & b); // 1
Q15. What is the ternary operator?
A shorthand if-else operator that returns one of two values based on a condition.
condition ? value_if_true : value_if_falseExample:
int age = 20;
String result = (age >= 18) ? "Adult" : "Minor";
Comments & Code Formatting
Q16. What are types of comments in Java?
- Single-line →
// - Multi-line →
/* */ - Documentation comment →
/** */
Example:
// Single-line
/* Multi-line */
/** Documentation comment */
Q17. Why are comments used?
To explain logic and improve code readability.
Q18. What are Java code formatting best practices?
- Use meaningful variable names
- Proper indentation
- Follow CamelCase
- Add comments where needed
- Keep methods short
Input / Output using Scanner and BufferedReader
Q19. How to take input using Scanner?
import java.util.Scanner;
class InputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
System.out.println("Age: " + age);
}
}
Q20. What are disadvantages of Scanner?
- Slower than BufferedReader
- Uses more memory
Q21. How to take input using BufferedReader?
import java.io.*;
class InputExample2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println("Name: " + name);
}
}
Q22. Which is faster – Scanner or BufferedReader?
BufferedReader is faster because it reads data in larger chunks.
Java Keywords & Identifiers
Q23. What are keywords?
- Reserved words in Java (cannot be used as variable names).
- There are 67 keywords in Java (including new ones).
Examples:
class, int, static, public, return, if, else, extends, final
Q24. What are identifiers?
Names given to:
- Variables
- Methods
- Classes
- Objects
- Packages
Example:
int age; // age is identifier
class Student{} // Student is identifier
Q25. What are rules for identifiers?
- Cannot start with digit
- Cannot use keywords
- Case-sensitive
- Allowed: letters, digits,
_,$
- Valid:
myVar,_count,$value - Invalid:
1total,class,my-var
