Java Basic Interview Questions

50 most frequently asked Java Basic Interview Questions.

1. What is Java?

Answer: Java is a high-level, object-oriented programming language developed by Sun Microsystems, now owned by Oracle.

2.What are the key features of Java?

Answer: Key features of Java include platform independence, object-oriented, robust, secure, and high performance.

3. What is a class in Java?

Answer: A class is a blueprint or template for creating objects in Java.

4. What is an object in Java?

Answer: An object is an instance of a class, which represents a real-world entity.

5. Explain the difference between a class and an object.

Answer: A class is a blueprint, while an object is an instance of that class.

6. What is a data type in Java?

Answer: A data type defines the type of data a variable can hold.

7. How many primitive data types are there in Java?

Answer: Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean.

8. What is the default value of int data type in Java?

Answer: The default value of an int is 0.

9. What is a variable in Java?

Answer: A variable is a container that holds data or values.

10. What are the different types of variables in Java?

Answer: There are three types of variables in Java: instance variables, class variables, and local variables.

11. Explain the ‘final’ keyword in Java.

Answer: The ‘final’ keyword is used to make a variable, method, or class immutable or unchangeable.

12. What are operators in Java?

Answer: Operators are symbols used to perform operations on variables and values.

13. What is the ‘==’ operator used for in Java?

Answer: The ‘==’ operator is used to compare the values of two objects or variables.

14. What is a constructor in Java?

Answer: A constructor is a special method used to initialize objects.

15. What is the default constructor in Java?

Answer: If a class does not have any constructor, Java provides a default no-argument constructor.

16. What is method overloading in Java?

Answer: Method overloading is when a class has multiple methods with the same name but different parameters.

17. What is method overriding in Java?

Answer: Method overriding is when a subclass provides a specific implementation of a method defined in its superclass.

18. What is the ‘this’ keyword in Java?

Answer: ‘this’ is a reference to the current object, which is used to distinguish between instance variables and method parameters.

19. What is ‘super’ in Java?

Answer: ‘super’ is used to call the superclass’s constructor, method, or variable.

20. What is a package in Java?

Answer: A package is a namespace that organizes classes, interfaces, and sub-packages.

21. What is the ‘import’ statement used for?

Answer: The ‘import’ statement is used to import classes or packages in Java.

22. Explain the ‘static’ keyword in Java.

Answer: ‘static’ is used to define a class-level variable or method that belongs to the class, not an instance.

23. What is an instance variable in Java?

Answer: An instance variable is a variable that belongs to an instance of a class.

24. What is a class variable in Java?

Answer: A class variable is a variable that belongs to a class and is shared by all instances of the class.

25. Explain the ‘public’ access modifier.

Answer: ‘public’ allows unrestricted access to a class, method, or variable from any other class.

26. What is the ‘private’ access modifier?

Answer: ‘private’ restricts access to only within the same class.

27. Explain the ‘protected’ access modifier.

Answer: ‘protected’ allows access within the same package and by subclasses outside the package.

28. What is the ‘default’ (package-private) access modifier?

Answer: ‘default’ allows access within the same package but not outside the package.

29. What is a method signature in Java?

Answer: The method signature consists of the method name and the parameter types.

30. What is method chaining in Java?

Answer: Method chaining is a technique of invoking multiple methods on an object in a single line.

31. What is the ‘null’ keyword used for?

Answer: ‘null’ is used to represent the absence of a value or an uninitialized object.

32. What is a ‘NullPointerException’ in Java?

Answer: A ‘NullPointerException’ is an exception thrown when you try to access or perform operations on a null object.

33. What is a compiler in Java?

Answer: A compiler is a software that translates Java source code into bytecode.

34. What is the Java Virtual Machine (JVM)?

Answer: JVM is the runtime environment that executes Java bytecode.

35. What is the main method in Java, and why is it necessary?

Answer: The main method is the entry point for a Java program and is required to execute the program.

36. What is the purpose of the ‘String’ class in Java?

Answer: The ‘String’ class is used to represent and manipulate strings in Java.

37. How do you create an object in Java?

Answer: You create an object using the ‘new’ keyword followed by the class constructor.

38. Explain the difference between ‘==’, ‘equals()’, and ‘hashCode()’ for object comparison.

Answer: ‘==’ compares object references, ‘equals()’ compares object content, and ‘hashCode()’ returns a unique identifier.

39. What is garbage collection in Java?

Answer: Garbage collection is the process of automatically reclaiming memory occupied by objects that are no longer in use.

40. What is a constructor chaining in Java?

Answer: Constructor chaining is calling one constructor from another constructor within the same class using ‘this’ or ‘super’.

41. What is the ‘instanceof’ operator used for?

Answer: ‘instanceof’ checks if an object is an instance of a particular class or interface.

42. What is a ternary operator in Java?

Answer: The ternary operator (?:) is a shorthand way of writing an if-else statement.

43. What is the ‘break’ statement used for in Java?

Answer: ‘break’ is used to exit a loop or switch statement prematurely.

44. Explain the ‘continue’ statement in Java.

Answer: ‘continue’ is used to skip the current iteration of a loop and continue to the next iteration.

45. What is the ‘switch’ statement used for in Java?

Answer: ‘switch’ is used for multi-way branching based on the value of an expression.

46. What is the ‘for-each’ loop in Java?

Answer: The ‘for-each’ loop is used to iterate through elements in an array or collection.

47. What is an infinite loop in Java, and how do you avoid it?

Answer: An infinite loop is a loop that never terminates. To avoid it, ensure that the loop condition eventually becomes false.

48. What is type casting in Java?

Answer: Type casting is the process of converting one data type to another, either implicitly or explicitly.

49. What is the ‘StringBuilder’ class in Java, and how is it different from ‘String’?

Answer: ‘StringBuilder’ is a mutable class for string manipulation, whereas ‘String’ is immutable.

50. What is a ‘try-catch’ block, and how is it used for exception handling?

Answer: A ‘try-catch’ block is used to catch and handle exceptions that may occur in the ‘try’ block.

All Coding Handwritten Notes

Browse Handwritten Notes