0% found this document useful (0 votes)
6 views15 pages

Java Interview Q&A: Core Concepts Explained

The document contains a comprehensive list of 27 Java-related questions and answers, covering topics such as Java architecture, core concepts, variables, methods, and object-oriented programming principles. Each question is followed by detailed answers that explain fundamental Java concepts, including JDK, JRE, keywords, identifiers, variables, methods, constructors, and inheritance. The document serves as a study guide for interview preparation, ensuring a thorough understanding of Java fundamentals.

Uploaded by

K C Darshan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Java Interview Q&A: Core Concepts Explained

The document contains a comprehensive list of 27 Java-related questions and answers, covering topics such as Java architecture, core concepts, variables, methods, and object-oriented programming principles. Each question is followed by detailed answers that explain fundamental Java concepts, including JDK, JRE, keywords, identifiers, variables, methods, constructors, and inheritance. The document serves as a study guide for interview preparation, ensuring a thorough understanding of Java fundamentals.

Uploaded by

K C Darshan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Final Definitive Q&A Filtered by Handwritten

Notes (27 Q&A)


This is the final, un-tabulated list, ensuring continuous reading flow for your interview practice,
based only on your PDF notes.

1. Core Java and Architecture


Q1. What is JDK?​
Answer (3 Points):

1.​ JDK (Java Development Kit) is a development kit that consists of all the libraries and
utilities (P.13) of a Java program.
2.​ It provides the full environment needed for program development (P.11).
3.​ It contains the JRE and the Java Compiler (Javac).

Q2. What is JRE?​


Answer (3 Points):

1.​ JRE (Java Runtime Environment) is the environment setup provided to run the Java
program (P.13).
2.​ It contains the JVM and the necessary core class libraries.
3.​ It provides the runtime infrastructure.

Q3. Java is Platform independent. Explain.​


Answer (5 Points):

1.​ Java is platform-independent due to the concept of Bytecode (.class file) (P.12).
2.​ This principle is called "Write Once Run Anywhere" (WORA) (P.10).
3.​ The Compiler translates the source code (.java file) into generic, intermediate Bytecode
(P.12).
4.​ The JVM on the target machine (whether Windows, Linux, etc.) executes this same
Bytecode (P.12).
5.​ This means the developer doesn't have to compile the code for every operating system.

Q4. What is the signature of the main method?​


Answer (5 Points):

1.​ The complete signature of the main method is: public static void main(String[] args)
(P.13, 15).
2.​ public: It is an Access Specifier, making the method accessible outside the class (P.22).
3.​ static: The method can be called without creating an object of the class (P.27).
4.​ void: The method does not return any value (P.22).
5.​ It accepts an array of String arguments (P.15).

2. Tokens: Keywords, Identifiers, and Literals


Q5. What is a keyword? List some keywords.​
Answer (4 Points):

1.​ A keyword is a predefined word that has its own meaning (P.3).
2.​ There are 50 Keywords in Java (P.6).
3.​ Examples: class, public, static, void, int, boolean, if, else, return (P.6).

Q6. What is an identifier?​


Answer (4 Points):

1.​ An identifier is the name given to the Java program elements, such as a class, method,
or variable (P.3, 5).
2.​ It must start with a letter, underscore, or dollar sign (P.5).
3.​ It cannot be a Keyword or contain whitespace (P.6).

Q7. What are literals?​


Answer (4 Points):

1.​ Literals are the values which are given for a variable in a Java program (P.7).
2.​ Types include Number (Integer/Decimal), String, Character, and Boolean (P.4).

3. Variables, Scope, and Types


Q8. What is a Variable?​
Answer (2 Points):

1.​ A variable is a memory location used to store some values (P.16).


2.​ The value can be changed (re-initialized) multiple times (P.16).

Q9. Mention all the primitive data types in java.​


Answer (4 Points):

1.​ Integer types: byte, short, int, long (P.16).


2.​ Decimal types: float, double (P.17).
3.​ Character type: char (P.17).
4.​ Boolean type: boolean (P.17).

Q10. What are Reference Variables?​


Answer (3 Points):

1.​ A reference variable is a special type of variable that stores the object address (P.38).
2.​ It is used to access non-static members of a class (P.29).
3.​ Its syntax is ClassName RefName = new ClassName(); (P.38).

Q11. What are Global variables?​


Answer (3 Points):

1.​ Global variables are declared inside the class but outside the method (P.21).
2.​ Their scope is class-wide (P.21).
3.​ They are classified into static and non-static (P.21).

Q12. What will be the default value for global variables?​


Answer (4 Points):

1.​ Global variables have default values since they are not local variables (P.21).
2.​ int, byte, etc., default to 0 (P.21).
3.​ boolean defaults to false (P.21).
4.​ String and other Reference Variables default to null (P.21).

Q13. What are the differences between local and global variable?​
Answer (4 Points):

1.​ Location: Local is inside method; Global is inside class (P.21).


2.​ Scope: Local is method-only; Global is class-wide (P.21).
3.​ Default Value: Local has NO default value (must initialize); Global has default values
(P.21).
4.​ Static/Non-Static: Local cannot be static/non-static; Global can be (P.21).

4. Methods, Objects, and Members


Q14. What is a method?​
Answer (3 Points):

1.​ A method (Function Member) is a block of statements that executes when called
(P.22).
2.​ Its role is to perform some operations (P.22).
3.​ General syntax: [Access Specifier] [Modifier] [Return Type] Name(Parameters) { ... }
(P.22).
Q15. What is an Object?​
Answer (3 Points):

1.​ An object is a real-time entity (P.45).


2.​ It has state (variables) and behaviour (methods) (P.45).
3.​ It is created from a class (the blueprint) (P.45).

Q16. What is a class?​


Answer (3 Points):

1.​ A class is a blueprint or template (P.13, 14).


2.​ It is used to create an object (P.13).
3.​ It can be seen as a collection of templates (P.13).

Q17. What are static members?​


Answer (4 Points):

1.​ Static members are declared with the static keyword (P.27).
2.​ They belong to the class (P.27).
3.​ Only a single copy exists, stored in the Static Pool (P.27, 35).
4.​ They are accessed using the Class Name (P.27).

Q18. What are non-static members?​


Answer (4 Points):

1.​ Non-static members are declared without the static keyword (P.29).
2.​ They belong to the Object (P.29).
3.​ Multiple copies exist, stored in the Heap Memory (P.29, 35).
4.​ They must be accessed using a Reference Variable (P.29).

Q19. How can you access a static global variable of a class?​


Answer (2 Points):

1.​ Static variables are accessed using the Class Name (P.27).
2.​ Syntax: [Link] (P.27).

Q20. How can you access non-static global variables of a class?​


Answer (3 Points):

1.​ Non-static variables are accessed using a Reference Variable (P.29).


2.​ This first requires object creation in the static context (P.29).
3.​ Syntax: [Link] (P.29).

5. Constructors and OOPS


Q21. What is a constructor?​
Answer (5 Points):

1.​ A constructor is a special method (P.43).


2.​ Used to initialize the data members (variables) of a class/object (P.43).
3.​ Name must be the same as the class name (P.43).
4.​ No return type (not even void) (P.43).
5.​ Always non-static (P.43).

Q22. What are two types of constructors?​


Answer (2 Points):

1.​ Pre-defined Constructor (Default) (P.43).


2.​ User-defined Constructor (P.43).

Q23. What is Inheritance?​


Answer (3 Points):

1.​ Inheritance is the mechanism of transferring properties from one class to another
(P.41).
2.​ The Super Class provides properties to the Sub Class (P.41).

Q24. What are the different types of inheritance?​


Answer (5 Points):

1.​ Single-level (P.41)


2.​ Multi-level (P.41)
3.​ Hierarchical (P.41)
4.​ Multiple (P.41)
5.​ Hybrid (P.41)

Q25. What is call by value and call by reference?​


Answer (4 Points):

1.​ Call by Value (Pass by Value): Passing primitive data types as arguments to a
method (P.44).
2.​ Call by Reference (Pass by Reference): Passing a Reference Variable (object) as an
argument (P.44).
3.​ In Pass by Value, a copy is used; in Pass by Reference, the memory address is used.

Final Program Example (Demonstrating Core Syntax)


Q26. Program: Demonstrate the method definition and access rules.​
Answer (Code Example):

code Java
downloadcontent_copy
expand_less
class AccessDemo { // P.13 Class Name (Identifier)

// Global Static Variable (P.21) - accessed by Class Name


public static int COUNT = 50;

// Non-Static Method (P.29) - accessed by Object


public void printInfo(int x) { // x is a Parameter (P.25)
// Accessing Static Variable from Non-Static context (direct access allowed)
[Link]("Non-Static method. Count: " + COUNT);
}

// Main Method: Static Context (P.15)


public static void main(String[] args) {

// 1. Accessing Static Variable (Q19)


[Link]("Static Count: " + [Link]);

// 2. Creating an Object (Required for Non-Static access)


// Syntax: ClassName RefVar = new ClassName(); (P.38)
AccessDemo a = new AccessDemo();

// 3. Calling Non-Static Method (Q20)


[Link](10); // 10 is the Argument passed (P.25)
}
}

PART _2
SECTION 1: Java Basics and Architecture
Q1. What is the fundamental definition of Java and its primary characteristic?​
Answer (5 Points):

1.​ Java is defined as a high-level, object-oriented programming language (P.1).


2.​ Its primary characteristic is platform independence or WORA (Write Once Run Anywhere) (P.1).
3.​ It is used to interact with machines and build software technologies (P.1).
4.​ It was developed by James Gosling in 1991 (P.1).
5.​ It is considered a popular programming language (P.1).

Q2. Describe the core responsibility of the JDK, JRE, and JVM.​
Answer (5 Points):

1.​ JDK (Java Development Kit): Consists of all libraries and utilities necessary for program
development (P.11).
2.​ JRE (Java Runtime Environment): The total environment setup required to run the Java program
(P.11, 13).
3.​ JVM (Java Virtual Machine): The core component wholly responsible for executing the Bytecode
(P.11).
4.​ JVM also contains the Interpreter for line-by-line execution (P.10).

Q3. Explain the Java Compilation process.​


Answer (5 Points):

1.​ The source code (.java file) is given to the Compiler (javac) (P.10).
2.​ The Compiler checks the code for rules and syntax violations (P.11).
3.​ If successful, the Compiler translates the code into Bytecode (P.10).
4.​ This Bytecode is saved in a .class file (P.10).
5.​ If syntax rules are violated, a Compile Time Error is generated (P.12).

Q4. What is Bytecode, and what is its role in "Write Once Run Anywhere"?​
Answer (5 Points):

1.​ Bytecode is an intermediate code format (P.10).


2.​ It is generated by the Compiler and saved in the .class file (P.12).
3.​ Its format cannot be understood by neither humans nor machines directly (P.12).
4.​ The JVM translates this generic Bytecode into the specific machine code for the target OS (P.10).
5.​ This means the single .class file can be executed "Anywhere" there is a JVM.

Q5. How does a Runtime Error occur, and give one documented example?​
Answer (5 Points):

1.​ A Runtime Error occurs during program execution (after compilation) (P.12).
2.​ It is triggered when the Interpreter/JVM encounters an abnormal occurrence or statement (P.12).
3.​ It is a logical failure, not a syntax failure.
4.​ The documented example is the Arithmetic Exception (e.g., trying to divide by zero: 1/0) (P.12).
5.​ The JVM will halt the program and provide the exception details.

Q6. What are the four primary Structural Types in Java?​


Answer (5 Points):

1.​ Class: A blueprint or template used to create an object (P.13, 14).


2.​ Interface (P.13).
3.​ Enum (P.13).
4.​ Annotations (P.13).

SECTION 2: Tokens, Identifiers, and Syntax


Q7. What is a Token? List all six types.​
Answer (5 Points):

1.​ A Token is the smallest individual unit of a Java program (P.2, 5).
2.​ The six types are: Identifiers, Keywords, Literals, Operators, Separators, and Comments (P.2, 5).

Q8. List the three critical rules regarding how an Identifier name must begin.​
Answer (5 Points):

1.​ Identifiers are the names given to classes, methods, or variables (P.3, 5).
2.​ The name must first begin with a letter (A-Z, a-z) (P.5).
3.​ The name can also begin with an underscore (_) (P.5).
4.​ The name can also begin with a dollar sign ($) (P.5).

Q9. Why are Identifiers restricted from being Keywords or containing whitespace?​
Answer (5 Points):

1.​ Keyword Restriction: Identifiers cannot be a Keyword because Keywords (all 50) have a predefined
meaning to the compiler (P.3, 6).
2.​ The compiler would mistake the custom name for a core language instruction.
3.​ Whitespace Restriction: Identifiers cannot contain whitespace (P.6).
4.​ The whitespace signals the end of the Identifier name, which the compiler would mistake for two
separate words (e.g., Add 2 is read as two tokens).

Q10. Define Literals. List the four types with a distinct example.​
Answer (5 Points):

1.​ Literals are the values which are given to a variable, used to perform operations (P.7).
2.​ Number Literal: 10 (Integer) or 5.5 (Decimal) (P.4).
3.​ String Literal: "Hello Java" (Sequence in double quotes) (P.4).
4.​ Character Literal: 'a' (Single letter in single quotes) (P.4).
5.​ Boolean Literal: True or False (Logical values) (P.4).

Q11. List the 5 different symbols used as Separators.​


Answer (5 Points):

1.​ Braces: {} (Used to enclose class/method bodies) (P.9).


2.​ Brackets: [] (Used for arrays) (P.9).
3.​ Parentheses: () (Used for method parameters/expression grouping) (P.9).
4.​ Semicolon: ; (Used to terminate a statement) (P.9).
5.​ Comma: , (Used to separate list elements) (P.9).

Q12. What two Arithmetic Operator categories exist, and list the operators in each.​
Answer (5 Points):

1.​ The primary category is Arithmetic (P.8).


2.​ It is subdivided into: Multiplicative Operators (*, /, %) (P.8).
3.​ And: Additive Operators (+, -) (P.8).

Q13. List the Relational and Logical Operators.​


Answer (5 Points):

1.​ Relational/Comparison Operators: (<, >, <=, >=, ==, !=, instanceof) (P.8).
2.​ Logical Operators: Used for connecting conditions (&& for Logical AND, || for Logical OR) (P.8).

SECTION 3: Variables and Scope


Q14. Define a Variable and name the two major data type categories.​
Answer (5 Points):

1.​ A Variable is a memory location used to store some values (P.16).


2.​ The stored value can be changed (re-initialized) multiple times (P.16).
3.​ The two major data type categories are Primitive Data Types and Non-Primitive Data Types (P.16).
4.​ Example Primitive: int, byte, boolean (P.16).
5.​ Example Non-Primitive: String, Arrays, or any class type (P.16).

Q15. Explain the distinction in initialization rules between Local Variables and Global Variables.​
Answer (5 Points):

1.​ Global Variables have default values if uninitialized (e.g., int=0, String=null) (P.21).
2.​ Therefore, explicit initialization is optional for global variables (P.21).
3.​ Local Variables have no default values (P.21).
4.​ Therefore, a local variable must be explicitly initialized before it is used in any operation (P.21).

Q16. What is the difference in scope for Local vs. Global Variables?​
Answer (5 Points):

1.​ Local Variable is declared inside the method (P.21).


2.​ Its scope is limited from the start of the method to the end of the method (P.21).
3.​ Global Variable is declared inside the class but outside the method (P.21).
4.​ Its scope extends from the starting of the class to the end of the class (P.21).

Q17. Which variable type cannot be classified as static or non-static?​


Answer (5 Points):

1.​ Local Variables cannot be classified into static or non-static (P.21).


2.​ The static modifier is reserved for class-level (Global) members.

SECTION 4: Methods, Constructors, and Inheritance


Q18. Define a Method and state the significance of the void return type.​
Answer (5 Points):

1.​ A Method (Function Member) is a block of statements that executes when called (P.22).
2.​ Its role is to store logic and perform some operations (P.22).
3.​ Methods are key to achieving Polymorphism.
4.​ The Return Type defines the type of value the method will send back to the caller (P.22).
5.​ void means the method does not return any value (P.22).

Q19. Define a Constructor and list its three strict rules.​


Answer (5 Points):

1.​ Definition: A special method used to initialize the data members (variables) of a class/object (P.43).
2.​ Rule 1 (Name): Name must be the same as the class name (P.43).
3.​ Rule 2 (Return Type): No return type (not even void) (P.43).
4.​ Rule 3 (Modifier): Always non-static (P.43).
5.​ It is automatically invoked during object creation (P.43).

Q20. Define Inheritance. What is the difference between Super Class and Sub Class?​
Answer (5 Points):

1.​ Inheritance: The mechanism of transferring properties (variables and methods) from one class to
another (P.41).
2.​ Super Class (Base Class): The class whose properties are transferred (P.41).
3.​ Sub Class (Child Class/Derived Class): The class that receives the properties (P.41).
4.​ It models the "is-a" relationship (e.g., Student is a Person).
5.​ It is a core concept for achieving code reusability.

Q21. Differentiate between Pass by Value and Pass by Reference for method calls.​
Answer (5 Points):

1.​ Pass by Value: Invoking a method by passing primitive data types (e.g., int, double) (P.44).
2.​ Effect (Value): The method works on a copy; the original value remains unchanged outside the
method.
3.​ Pass by Reference: Invoking a method by passing a Reference Variable (object) (P.44).
4.​ Effect (Reference): The method is given the object's memory address; any changes made to the
object inside the method affect the original object.

SECTION 5: Static, Non-Static, and Memory Management


Q22. What is the fundamental difference in definition between a Static Member and a Non-Static
Member?​
Answer (5 Points):

1.​ Static Member: Declared with the static keyword (P.27). It belongs to the Class.
2.​ Non-Static Member: Declared without the static keyword (P.29). It belongs to the Object (Instance).

Q23. Describe the memory storage for Static Members and Non-Static Members.​
Answer (5 Points):

1.​ Static Storage (P.35): Stored in the Method Area / Static Pool memory.
2.​ Only a Single Copy exists, which is shared across all objects (P.27).
3.​ Non-Static Storage (P.35): Stored in the Heap Memory as part of an object (P.35).
4.​ Multiple Copies exist, one unique set for every object instance created (P.29).

Q24. What is a Reference Variable, and what is its role in non-static access?​
Answer (5 Points):

1.​ A Reference Variable is a special type of variable that stores the object address (P.38).
2.​ The variable itself is stored on the Stack memory (P.35).
3.​ It provides the mandatory link to the actual object data in the Heap memory (P.29, 35).
4.​ It is used to access all non-static variables and methods of that object ([Link]) (P.29).

Q25. What rule must a static method follow to access a non-static member? (Q30)​
Answer (5 Points):

1.​ The static method (like main) must first create an object of the class using the new operator (P.29).
2.​ The address of this object must be saved in a Reference Variable (P.38).
3.​ The non-static member is then called using the syntax: [Link](); (P.29).

SECTION 6: Programming Syntax and Patterns


Q26. Program Pattern: How to call a Non-Static Method from the Static main Method. (P.29, 30)​
Answer (Syntax & Code):

1.​ Rule: Must create an object using a Reference Variable.


2.​ Syntax: ClassName RefVar = new ClassName(); then [Link]();

code Java
downloadcontent_copy
expand_less
class Sample {
// 1. Non-Static Method
public void add(int x, int y) {
[Link](x + y);
}

public static void main(String[] args) {


// 2. Object Creation & Reference Variable (Static to Non-Static Access Rule)
Sample s1 = new Sample();

// 3. Invocation of Non-Static method via reference


[Link](10, 5);
}
}
Q27. Program Pattern: Static Method with Return Type and Parameters. (P.26)​
Answer (Syntax & Code):

1.​ Goal: Static method must return a calculated value.


2.​ Definition Syntax: public static [ReturnType] [Name] ([Parameters]) { return value; }

code Java
downloadcontent_copy
expand_less
class MathDemo {
// 1. Static method with 'int' Return Type
public static int multiply(int a, int b, int c) {
int res = a * b * c;
return res;
}

public static void main(String[] args) {


// 2. Variable (x) is required to capture the returned value
int x = multiply(2, 3, 4);
[Link]("Result: " + x); // Output: 24
}
}

Q28. Program Pattern: The If-Else-If Conditional Block (Marks Logic). (P.20, 29)​
Answer (Syntax & Code):

1.​ Logic: Provides control flow by checking conditions sequentially.


2.​ Syntax: if (Condition) { ... } else if (Condition) { ... } else { ... }

code Java
downloadcontent_copy
expand_less
class GradeChecker {
public static void main(String[] args) {
int marks = 75;

if (marks >= 85) {


[Link]("1st Class with Distinction");
} else if (marks >= 60) {
[Link]("1st Class"); // marks is 75 (this block executes)
} else if (marks >= 35) {
[Link]("Pass");
} else {
[Link]("Fail");
}
}
}

Q29. Program Pattern: Pass by Reference. (P.44)​


Answer (Syntax & Code):

1.​ Rule: An object reference is passed as the argument.


2.​ Definition Syntax: The parameter must be of the ClassName type.

code Java
downloadcontent_copy
expand_less
class Data {
int value = 100; // Global state
}
class PassByRef {
// 1. Static method parameter must be the object type (Data d)
public static void printValue(Data d) {
[Link]("Value in method: " + [Link]);
[Link] = 500; // Modification will affect original d1
}

public static void main(String[] args) {


// 2. Create Object & Reference Variable
Data d1 = new Data();

// 3. Call method, passing the object reference (d1)


printValue(d1);

// 4. Output: Original [Link] is now 500 due to Pass By Reference.


}
}

Q30. Syntax List: All Invocation and Declaration Forms​


Answer (Reference to all syntaxes):

1.​ Variable Declaration (P.17): DataType VariableName;


2.​ Variable Initialization (P.17): VariableName = value;
3.​ Reference Variable Declaration (P.38): ClassName RefVar = new ClassName();
4.​ Static Method Call (P.27): [Link](); or StaticMethod();
5.​ Non-Static Method Call (P.29): [Link]();
6.​ Parameter Definition (P.25): (DataType VariableName, ...)

You might also like