CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Interview Questions for Freshers

Use these questions to practise explaining Java in your own words. After answering, write a tiny program that demonstrates the rule and one input that tests its boundary. The answers are starting points for discussion, not lines to memorize.

1. What is Java?

Answer: Java is a programming language whose code is commonly compiled into bytecode for a JVM. A compatible JVM lets the same bytecode run on different systems, provided the program does not rely on unavailable platform-specific features. The compiler checks the source, while the JVM loads and executes the resulting classes.

The main key features of Java are its simple syntax, platform independence, object-oriented programming, robustness, security, and multithreading capabilities.

2. What are the main features of Java?

Answer: The main features described in this tutorial are simple syntax, platform independence, object-oriented programming, robustness, security, and multithreading. Java uses automatic garbage collection to reduce memory leaks and crashes, and its JVM restricts unauthorized access to memory. Its multithreading capability allows multiple tasks to be created, run, and managed at the same time.

3. What is the JVM?

Answer: JVM stands for Java Virtual Machine. It executes Java bytecode and translates it into machine code specific to the operating system, enabling Java programs to run on different operating systems. After javac creates a .class file, the JVM takes that bytecode and converts it into code that the computer's processor can execute.

4. How does Java achieve platform independence?

Answer: The Java compiler (javac) compiles .java source files into .class bytecode files. The JVM then translates the bytecode into machine code specific to the operating system. For example, HelloWorld.java becomes HelloWorld.class, which can be executed through the JVM on Windows, macOS, or Linux without rewriting the Java code.

5. What is the JRE?

Answer: JRE stands for Java Runtime Environment. After a Java application is developed, the JRE provides everything needed to run Java applications. It contains the JVM, which converts bytecode into operating-system-specific machine language, and core Java libraries that provide pre-written classes and methods for common functionality.

6. What is the JDK?

Answer: JDK stands for Java Development Kit. It is used to write, compile, debug, and run Java programs, and it contains the JRE and development tools. In this tutorial, javac is the compiler, java is the launcher, javadoc generates HTML documentation, jar packages class files and resources, and jdb helps inspect code line by line.

7. What is a variable in Java?

Answer: A variable is a container that stores data or a value. It has a name, type, and value, and its type tells the size and structure of the variable's memory. For example, in int myVariable = 10, myVariable is the name, int is the type, and 10 is the value.

8. What are the types of variables in Java?

Answer: Java has three types of variables: local variables, instance variables, and static variables. The tutorial demonstrates local variables inside methods, constructors, and blocks, and explains that their scope is limited to the place where they are declared.

9. What is a local variable?

Answer: A variable declared inside a method, constructor, or block is called a local variable. It is created when that method, constructor, or block executes and is destroyed when execution finishes. It can be accessed only in that scope, and it must be initialized explicitly before it is used.

10. What are the categories of data types in Java?

Answer: Java data types are divided into primitive types and reference, or non-primitive, types. A data type specifies what kind of value a variable can store and tells the size, range, and nature of the data stored in memory. The tutorial describes primitive types as basic types that hold single, simple values such as numbers or characters.

11. What are the primitive data types in Java?

Answer: Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean. The examples include int marks = 95, double pi = 3.14159, char grade = 'A', and boolean isPassed = true.

12. What is a method in Java?

Answer: A method is a block defined within a class where the functionality or behavior of an object is written. It performs a specific task and can be reused multiple times, which improves code reusability and maintainability. A method can be a void method, return a value such as int, take parameters such as String name, or use no parameters.

13. What is a method signature?

Answer: A method signature is the combination of only the method name and parameter lists. The method syntax in the tutorial also includes an access modifier, return type, and method body, but those are not part of the method signature described there.

14. What is inheritance in Java?

Answer: Inheritance is a feature in which one class inherits properties, such as variables, and behavior, such as methods, from another class. The child class inherits from the parent class, and the extends keyword is used to inherit a class. The tutorial's Dog extends Animal example reuses the parent's variables and method, while its UpiPayment extends Payment example enhances the parent's payment method. Its benefits include code reuse, method enhancement, and organized code structure.

15. How is a Java program compiled and executed?

Answer: First, save the source file with the .java extension, such as HelloWorld.java, and make the file name the same as the class name. Run javac HelloWorld.java to compile it and generate HelloWorld.class, which contains bytecode. Then run java HelloWorld to execute the compiled program through the JVM and see the Hello, World! output in the command prompt.

16. What is a class in Java?

Answer: A class is a template or blueprint from which objects are created. It contains the object's properties, represented by variables, and behavior, represented by methods. A Human class can define properties such as name, age, and gender, along with methods such as eat(), sleep(), and walk().

17. What members can a Java class contain?

Answer: The class lesson lists variables, constructors, methods, and blocks as class members. Variables store the class properties, while methods define its behavior. The class body is written between curly braces.

18. What is an object in Java?

Answer: An object is an instance of a class that takes memory during runtime. It has state stored in variables, behavior defined by methods, and a unique identity. For example, person1 can be an object created from the Human class.

19. How do you create an object in Java?

Answer: An object is created with the syntax ClassName objectName = new ClassName();. The new keyword creates memory for the object in the heap and instantiates the class. The constructor initializes the object and has the same name as the class.

20. What is object instantiation?

Answer: The creation of an object is called instantiation. In the tutorial example, Human person1 = new Human(); creates an object, then values are assigned to its name, age, and gender properties and its eat(), sleep(), and walk() methods are called.

21. What is a constructor in Java?

Answer: A constructor is similar to a method because it has a name and can take parameters, but its name is always the same as the class name. It initializes the new object's instance state during object creation, is called automatically when an object is created, and has no return type, not even void.

22. What are the types of constructors described in the tutorial?

Answer: Constructors can be written with no parameters or with parameters. A default constructor specifically means the constructor the compiler supplies when an ordinary class declares none. A programmer-written no-argument constructor is not a default constructor in this precise sense.

23. What is a default constructor?

Answer: A default constructor is supplied by the compiler when an ordinary class declares no constructor. It has no parameters. If the programmer declares any constructor, the compiler does not supply this default constructor.

24. What is a parameterized constructor?

Answer: A parameterized constructor has parameters and initializes variables with values during object creation. For example, BankAccount(String name, double initialBalance) assigns the account holder and balance when new BankAccount("Ashish", 5000) is used.

25. What is constructor overloading?

Answer: Constructor overloading means defining multiple constructors in one class. The constructors must differ by the number, type, or order of their parameters. The tutorial shows constructors that accept one parameter, two parameters, different types, and different parameter orders.

26. What does the this keyword refer to?

Answer: The this keyword is a reference variable that holds the address of the current object. It is used inside methods and constructors to refer to the current object. In this.message = message, it distinguishes the instance variable from the constructor parameter with the same name.

27. How does this distinguish variables with the same name?

Answer: When a local variable and an instance variable have the same name, the name by itself refers to the local variable inside the method. this.message refers to the instance variable. The tutorial prints both values to show the difference between the local and instance variables.

28. How is this used for constructor chaining?

Answer: When multiple constructors exist in the same class, this() calls another constructor in that class. The tutorial shows a no-argument constructor calling this("Hello from parameterized constructor", 100). In Java 21 and earlier, an explicit this() call must be the first constructor statement. Java 25 permits certain statements before it, subject to early-construction restrictions.

29. What is the super keyword used for?

Answer: The super keyword selects a superclass constructor or an accessible superclass member for the current object. It does not refer to a separate parent object. It is used to call the parent class constructor and method and to access parent class variables. For example, super.processPayment(amount) calls the parent implementation before the child adds its own logic.

30. How is super used with a constructor?

Answer: A child class can call a superclass constructor with super(). In the tutorial's Dog extends Animal example, creating a Dog object first invokes the Animal constructor and then the Dog constructor. If an ordinary class constructor has no explicit this(...) or super(...) call, Java normally inserts a no-argument super() call. An accessible matching superclass constructor must exist.

31. What is widening type casting?

Answer: Widening, or implicit, casting automatically converts a smaller data type into a larger data type. The compiler performs permitted widening conversions without an explicit cast. Widening int to float, or long to float or double, can lose precision. For example, int myInteger = 15; double myDouble = myInteger; produces 15.0.

32. What is narrowing type casting?

Answer: Narrowing, or explicit, casting converts a larger data type into a smaller data type using the cast operator. Data loss can occur. In the tutorial example, double x = 9.78; int y = (int) x; produces 9 because the decimal part is removed.

33. What is type promotion in Java calculations?

Answer: Type promotion is when Java automatically changes a smaller data type to a larger data type during a calculation. The tutorial shows two byte values being promoted to int before addition, producing an int result. It also explains that byte, short, and char are promoted to int.

34. Why might explicit casting change a calculation result?

Answer: Explicit casting can control how a calculation is performed. The tutorial shows 5 / 2 producing 2 because integer division discards the fractional part, while (float) 5 / 2 produces 2.5 by promoting the division to floating-point division.

35. What are control statements in Java?

Answer: Control statements change the sequential flow of execution. They can break the sequential flow, jump to another part of the program, or change the direction of execution based on a condition. The tutorial groups decision-making statements and looping statements among these controls.

36. What does an if statement do?

Answer: An if statement evaluates a condition or boolean expression. It executes its block only when the expression returns true. The tutorial uses if (age >= 18) to print an eligibility message when the condition is true.

37. How does an if-else statement work?

Answer: An if-else statement has two blocks. The first block executes when the condition is true; otherwise, the else block executes. In the tutorial example, num % 2 == 0 prints Even number when true and Odd number otherwise.

38. What is an else-if statement used for?

Answer: An else-if statement checks multiple conditions one after another. In the tutorial's marks example, the conditions select Grade A for marks of 90 or more, Grade B for marks of 75 or more, Grade C for marks of 50 or more, and Fail otherwise.

39. What are loops used for in Java?

Answer: Loops run a block of code repeatedly until a condition becomes false. The tutorial says this allows code to be reused, reduces code duplication, and improves code reusability. The listed loops include while, do-while, for, nested, and enhanced for loops.

40. How does a while loop work?

Answer: A while loop checks its condition first and continues executing while that condition is true. In the tutorial example, number starts at 1, is printed, and is increased until it becomes 6. If the value is not increased, the condition can remain true and the loop can run forever.

41. How is a do-while loop different from a while loop?

Answer: A do-while loop executes its block first and checks the condition afterward. Because of this order, it runs the block at least once even if the condition is false. The tutorial increments number after printing and continues while number <= 5.

42. What are the parts of a for loop?

Answer: The tutorial defines a for loop with initialization, condition, and update expressions: for (initialization; condition; update). Initialization sets the starting value, the condition is checked before each iteration, and the update changes the loop variable after each iteration. Its example prints numbers from 1 through 5.

43. What is a nested loop?

Answer: A nested loop is a loop inside another loop. The tutorial uses an outer row loop and an inner column loop, both running three times, to print row and column combinations. It describes nested loops as useful for rows and columns such as tables or patterns.

44. What is an enhanced for loop?

Answer: An enhanced for loop iterates through each element of an array or collection one by one without using an index. Its syntax is for (dataType variable : arrayOrCollection). The tutorial uses it to print each value in an integer array.

45. How do you display output in Java?

Answer: The simplest output statement shown in the tutorial is System.out.println("output message"). System.out is a built-in object used to display data on the standard output device, usually the console. println() prints text and moves to the next line, while print() keeps the cursor on the same line.

46. How do you take keyboard input in Java?

Answer: The tutorial uses the predefined Scanner class to receive keyboard input. It creates a scanner with new Scanner(System.in), reads values such as a name or age, displays the result, and closes the scanner with scanner.close().

47. What are some Scanner methods shown in the tutorial?

Answer: nextLine() reads an entire line, nextInt() reads a whole number, nextDouble() reads a decimal value, next().charAt(0) reads the first character from entered text, and nextBoolean() accepts true or false. The tutorial identifies System.in as the standard keyboard input stream.

48. What are formal and actual arguments in a method?

Answer: The arguments defined in a method definition are called formal arguments, while the arguments used when calling the method are called actual arguments. In the tutorial, number1 and number2 are formal arguments in myMethod(int number1, int number2), while 5 and 3 are actual arguments in obj.myMethod(5, 3).

49. What is an instance method?

Answer: A method defined inside a class without the static keyword is an instance method. It is associated with an object or instance, so an object must be created before the method can be called. The tutorial calls obj.myInstanceMethod() after creating a MyClass object.

50. What rules apply to a local variable before it is used?

Answer: A local variable must be initialized explicitly before it is used because the JVM does not initialize it automatically. It can be accessed only within the method, constructor, or block where it is declared. Local variables cannot use public, protected, or private. They may use final, which is a non-access modifier.

51. What is an operator in Java?

Answer: An operator is a special symbol that performs an operation on one or more variables or values. The variables or values used by an operator are called operands. The tutorial lists arithmetic, assignment, comparison, logical, unary, ternary, bitwise and shift, and instanceof operators.

52. What are arithmetic operators used for?

Answer: Arithmetic operators perform mathematical calculations on numeric values. The tutorial demonstrates addition, subtraction, multiplication, division, and modulus with 10 and 3, producing 13, 7, 30, 3, and 1 respectively.

53. What does the assignment operator do?

Answer: The assignment operator = assigns a value to a variable. For example, number = 5 stores 5 in number. The tutorial also demonstrates compound assignments such as +=, -=, *=, /=, and %=.

54. What are compound assignment operators?

Answer: Compound assignment operators combine an operation with assignment. For example, number += 3 is equivalent to number = number + 3, and number *= 3 is equivalent to number = number * 3. The tutorial also shows bitwise and shift compound assignments.

55. What do comparison operators return?

Answer: Comparison, or relational, operators compare two values or variables and return a boolean result, either true or false. The operators shown are ==, !=, >, <, >=, and <=. For example, with a = 10 and b = 20, a < b is true and a == b is false.

56. What are logical operators used for?

Answer: Logical operators combine boolean conditions and always return a boolean value. && returns true when both conditions are true, || returns true when at least one condition is true, and ! reverses the result. The tutorial uses age, marks, and an ID-card condition as examples.

57. What are unary operators?

Answer: Unary operators work with one operand. The tutorial includes unary plus, unary minus, increment ++, decrement --, and logical NOT !. Increment increases a value by one, decrement decreases it by one, and NOT reverses a boolean value.

58. What is the difference between pre-increment and post-increment?

Answer: Pre-increment, written ++initialValue, increases the value first and then prints it. Post-increment, written initialValue++, prints the current value first and increases it afterward. The tutorial's example shows the value becoming 11 after pre-increment, staying 11 for the post-increment output, and becoming 12 afterward.

59. What is the ternary operator?

Answer: The ternary operator is a short form of an if-else statement used to make a basic decision in one line. Its form is condition ? firstvalue : secondvalue. In the tutorial, (marks >= 40) ? "Pass" : "Fail" assigns either Pass or Fail to result.

60. What are bitwise and shift operators?

Answer: Bitwise and shift operators work directly on the binary representation of integer data types such as int, long, short, char, and byte. The tutorial lists AND &, OR |, XOR ^, NOT ~, left shift <<, right shift >>, and unsigned right shift >>>.

61. What does the bitwise AND operator do?

Answer: Bitwise AND returns 1 only when both corresponding bits are 1. The tutorial calculates 5 & 3 using 0101 & 0011, which produces 0001, or 1.

62. What does the bitwise OR operator do?

Answer: Bitwise OR returns 1 when at least one of the corresponding bits is 1. In the tutorial example, 5 | 3 uses 0101 | 0011 and produces 0111, or 7.

63. What does the bitwise XOR operator do?

Answer: Bitwise XOR returns 1 when the corresponding bits are different. The tutorial shows 5 ^ 3 as 0101 ^ 0011, producing 0110, or 6.

64. What do left-shift and right-shift operators do?

Answer: The left-shift operator << moves bits to the left, and the tutorial represents n << 1 as n * 2; for example, 5 << 1 produces 10. The right-shift operator >> moves bits to the right, and n >> 2 is shown as division by 4; for example, 20 >> 2 produces 5.

65. What is the unsigned right-shift operator?

Answer: The unsigned right-shift operator >>> is similar to >>, but it always fills the left side with zero. The tutorial demonstrates it with a negative integer and describes the zero-fill behavior.

66. What is the instanceof operator?

Answer: The instanceof operator checks at runtime whether an object is an instance of a specific class, subclass, or interface. It returns true when the object matches the specified type and false otherwise. The tutorial's examples check relationships such as a Cat with Dog, Animal, and Object.

67. What are comments in Java used for?

Answer: Comments add explanations or notes to code. The compiler ignores comments, so they do not affect program execution. The tutorial describes them as useful for helping developers understand code and maintain it.

68. What is a single-line comment?

Answer: A single-line comment starts with // and is used for a short explanation or note. The tutorial shows it before a statement and after a statement, such as System.out.println("Hello, World!"); // This is also a single-line comment.

69. What is a multi-line comment?

Answer: A multi-line comment starts with /* and ends with */. It can contain multiple lines and is used for longer explanations or notes. The tutorial shows a multi-line explanation placed above an output statement.

70. What are documentation comments?

Answer: Documentation comments start with /** and end with */. They are used to generate documentation and can include tags such as @param, @return, and @throws. The tutorial's calculator example uses these tags to describe method inputs, output, and an exception condition.

71. What is an identifier in Java?

Answer: An identifier is a unique name used for variables, methods, classes, interfaces, and packages. In the tutorial example, Students identifies a class, name identifies a variable, show identifies a method, and com.java.first.program contains package identifiers.

72. Which characters can an identifier contain?

Answer: According to the identifier lesson, an identifier can contain letters, digits, underscores, and dollar signs. Examples shown include name1, _name, $name, and first_name.

73. What restrictions apply to Java identifiers?

Answer: An identifier cannot start with a digit, cannot use a Java keyword, and cannot contain spaces. The lesson marks 5name, class, and My Age as invalid examples.

74. Are Java identifiers case-sensitive?

Answer: Yes. The tutorial states that identifiers are case-sensitive. It uses myage and MyAge as two different variable names.

75. Can multiple variables be declared in one statement?

Answer: Yes. The variables lesson shows int a, b, c; for declaring multiple variables in one line. It also shows separate initialization, such as int a = 10, b = 20, c = 30.

76. What is an instance variable?

Answer: An instance variable is defined inside a class but outside methods, blocks, and constructors. It is also called a non-static variable. Memory is allocated for it when an object is created, so multiple objects have separate copies.

77. What default values can instance variables receive?

Answer: The tutorial states that the JVM assigns default values to uninitialized instance variables. It gives 0 for int, 0.0 for double, false for boolean, and null for a reference type such as String.

78. Where is an instance variable stored according to the variables lesson?

Answer: The variables lesson states that an instance variable is created in heap memory. It also explains that each object receives its own copy, so two objects can have separate instance-variable data.

79. What is a static variable?

Answer: A static variable is declared with the static keyword inside a class but outside methods, blocks, and constructors. It is associated with the class and can be accessed using the class name without creating an object.

80. When is memory allocated for a static variable?

Answer: The variables lesson states that memory for a static variable is allocated when the class is loaded into memory by the JVM. The variable can then be accessed with its class name, such as MyClass.staticVariable.

81. Why can a static variable be shared by objects?

Answer: A static variable is shared among all objects of the class because there is one class-level copy. The tutorial uses an Employee example in which every employee shares the static company name MyCompany, instead of storing a separate company value for each object.

82. What is the difference between primitive and reference data types?

Answer: Primitive variables hold primitive values. Reference variables hold references to objects or null. Either kind of variable can be a local variable or an object field, so its type alone does not decide where its storage is allocated. Java does not expose a portable raw object address.

83. Which reference data types are listed in the tutorial?

Answer: The tutorial lists String, arrays, classes, interfaces, and objects as reference data types. It describes them as storing object references or complex data rather than simple values directly.

84. What is a static method?

Answer: A method declared with the static keyword is a static method. It is associated with the class and can be called by using the class name without creating an object. The tutorial calls MyClass.welcomeMessage() directly.

85. What is method overloading?

Answer: Method overloading means defining two or more methods with the same name inside the same class while using different parameters. The tutorial's show examples use different parameter counts, types, and orders to select different method implementations.

86. How can overloaded methods differ by parameter count?

Answer: Overloaded methods can have the same name but different numbers of parameters. The tutorial shows show(), show(String name), show(String name, int age), and show(String name, int age, String city) as four overloaded versions.

87. How can overloaded methods differ by parameter type?

Answer: Overloaded methods can use different parameter types. The tutorial shows show(int a), show(double a), and show(String a) in the same class, and calls each version with a matching argument type.

88. How can overloaded methods differ by parameter order?

Answer: Overloaded methods can have the same parameter types in a different order. The tutorial shows show(int a, String b) and show(String a, int b) as separate methods, and calls each with matching argument order.

89. How does Java choose an overloaded method?

Answer: The tutorial states that the compiler first tries an exact match for the method arguments. If no exact match is found, it applies type promotion, such as int to long and then double. If the call remains ambiguous, it produces a compile-time error.

90. What happens when overload resolution is ambiguous?

Answer: If more than one overloaded method can match and the compiler cannot choose one, the call produces a compile-time ambiguity error. The tutorial demonstrates this with show(int, double) and show(double, int) called using two integer arguments.

91. What does the break statement do?

Answer: The break statement immediately exits a loop and stops its execution. In the tutorial's enhanced for loop, the values 10 and 20 are printed, and the loop ends when number == 30, so later elements are not printed.

92. What does the continue statement do?

Answer: The continue statement skips the current loop iteration and moves to the next iteration. In the tutorial example, the value 30 is skipped, so the output contains 10, 20, 40, and 50.

93. What is the result of the arithmetic division in the operator example?

Answer: With firstNumber = 10 and secondNumber = 3, the arithmetic division expression firstNumber / secondNumber produces 3 in the tutorial example. The same pair produces a modulus result of 1.

94. What does the modulus operator return?

Answer: The modulus operator % returns the remainder of a division. The operators lesson uses 10 % 3, which produces 1, and labels it as the modulus result.

95. What is the difference between print() and println()?

Answer: Both methods display text on the console. The tutorial says println() moves the cursor to the next line after printing, while print() keeps the cursor on the same line so the next output continues there.

96. What does System.in represent in the input example?

Answer: System.in is the standard input stream, which the tutorial identifies as the keyboard. It is passed to new Scanner(System.in) so the scanner can read values entered by the user.

97. What does System.out represent in Java?

Answer: System.out is a built-in object used to display data on the standard output device, usually the console. The tutorial uses it with println() to display strings, numbers, and values read from input.

98. What is the difference between a class variable and an instance variable in the variables lesson?

Answer: A class variable is the static variable shared by all objects of a class. An instance variable belongs to an object, and each object receives its own copy. The tutorial's employee example shares company, while each employee has separate empId and name values.

99. How can a static method be called?

Answer: A static method can be called using the class name because it is associated with the class rather than a particular object. The tutorial uses MyClass.welcomeMessage() and explains that no object is required for this call.

100. What are the main jump statements covered in the tutorial?

Answer: The jump-statement lesson covers break and continue. break stops loop execution immediately, while continue skips the current iteration and proceeds to the next element or iteration.

See you in next page....🙂

← OverviewInterview Questions for 3 Years Experience →
← OverviewInterview Questions for 3 Years Experience →