1.
Hello World and Data Types
Q1: What is the purpose of the 'public' keyword in the main method?
A: The 'public' keyword makes the main method accessible from anywhere, which is necessary
for the JVM to call it when starting the program.
Q2: Why is 'static' used in the main method declaration?
A: The 'static' keyword allows the main method to be called without creating an instance of the
class, which is required as it's the entry point before any objects exist.
Q3: What are the 8 primitive data types in Java?
A: byte, short, int, long, float, double, char, and boolean.
Q4: What is the default value of a boolean data type?
A: The default value is 'false'.
Q5: What is the size of a 'double' data type in Java?
A: The 'double' data type occupies 8 bytes (64 bits) of memory.
2. Static, Local and Global Variables
Q1: What is the difference between static and instance variables?
A: Static variables belong to the class and are shared by all instances, while instance variables
are unique to each object instance.
Q2: Can we access a local variable outside its method?
A: No, local variables are only accessible within the method or block where they are declared.
Q3: What is the default value of an instance variable?
A: Instance variables have default values (0, 0.0, false, or null) depending on their type, unlike
local variables which must be initialized.
Q4: Where are static variables stored in memory?
A: Static variables are stored in the method area of the JVM memory.
Q5: Can we declare a static variable inside a method?
A: No, static variables must be declared at the class level, not inside methods.
3. String Operations
Q1: What is the difference between String.length() and String.length?
A: String.length() is a method for String objects while .length is a property for arrays. They both
return the length/size.
Q2: How can you concatenate two strings in Java?
A: Using the + operator or the concat() method (e.g., str1 + str2 or str1.concat(str2)).
Q3: What is the purpose of the substring() method?
A: The substring() method extracts a portion of a string based on specified begin and end
indexes.
Q4: Are strings in Java mutable or immutable?
A: Strings in Java are immutable - their values cannot be changed after creation.
Q5: What is the difference between substring(int) and substring(int, int)?
A: substring(int) returns from beginIndex to end of string, substring(int, int) returns from
beginIndex to endIndex-1.
4. Maximum of Three Numbers
Q1: What is the ternary operator and how can it be used to find the maximum?
A: The ternary operator (?:) is a shorthand if-else that can be used like: max = (a > b) ? a : b;
Q2: Can you use Math.max() to find the maximum of three numbers? How?
A: Yes, by nesting: Math.max(a, Math.max(b, c)).
Q3: What would happen if all three numbers are equal?
A: The program should return any one of them as they are all equal and maximum.
Q4: How would you modify the program to find the minimum instead?
A: By changing the comparison operators from > to < or using Math.min() instead of
Math.max().
Q5: What data type would you use for numbers with decimal points?
A: For decimal numbers, we should use float or double data types.
5. Odd or Even Check
Q1: What operator is used to check if a number is odd or even?
A: The modulus operator (%) is used to check the remainder when divided by 2.
Q2: What will be the result if we use a negative number?
A: The program will still work correctly as the modulus operator handles negative numbers
properly.
Q3: Can we use bitwise operators to check odd/even? How?
A: Yes, by checking the least significant bit: if ((num & 1) == 0) it's even, else odd.
Q4: What happens if we input a non-integer value?
A: The program will throw an InputMismatchException if proper input validation isn't
implemented.
Q5: How would you handle very large numbers that might exceed integer range?
A: We could use long instead of int, or BigInteger class for arbitrarily large numbers.
6. Constructors
Q1: What is the purpose of a constructor in Java?
A: Constructors initialize newly created objects and set initial values for object attributes.
Q2: What happens if you don't define any constructor in a class?
A: Java provides a default constructor with no parameters and no body.
Q3: Can a constructor be private? If yes, why would you make it private?
A: Yes, private constructors are used in singleton design patterns to prevent instantiation from
other classes.
Q4: What is constructor overloading?
A: Having multiple constructors with different parameter lists in the same class.
Q5: Can a constructor return a value?
A: No, constructors don't have return types, not even void.
7. Array of Objects
Q1: How is an array of objects different from an array of primitive types?
A: An array of objects contains references to objects, while primitive arrays contain actual
values.
Q2: What is the default value of elements in an object array?
A: The default value is null for each element in an object array.
Q3: How do you initialize an array of objects?
A: First create the array, then create each object and assign to array elements:
ClassName[] arr = new ClassName[size];
arr[0] = new ClassName();
Q4: Can an array hold objects of different types?
A: Only if they share a common superclass or interface, otherwise the array must be declared as
Object[].
Q5: What is the difference between length and size() for arrays and collections?
A: Arrays use .length (a property), while collections like ArrayList use .size() (a method).
8. Single Inheritance
Q1: What is inheritance in Java?
A: Inheritance is a mechanism where a child class acquires properties and behaviors of a parent
class.
Q2: How is inheritance implemented in Java?
A: Using the 'extends' keyword (class Child extends Parent).
Q3: What is method overriding?
A: When a subclass provides a specific implementation of a method already defined in its parent
class.
Q4: Can a class extend multiple classes in Java?
A: No, Java supports only single inheritance for classes.
Q5: What is the super keyword used for?
A: 'super' is used to refer to immediate parent class instance variables, methods, and constructors.
9. Multiple Inheritance using Interface
Q1: Why doesn't Java support multiple inheritance through classes?
A: To avoid the diamond problem and complexity that arises from multiple inheritance.
Q2: How does Java achieve multiple inheritance?
A: Through interfaces - a class can implement multiple interfaces.
Q3: What is the difference between an abstract class and an interface?
A: Abstract classes can have implemented methods and state, while interfaces (before Java 8)
could only have abstract methods.
Q4: Can an interface extend another interface?
A: Yes, an interface can extend multiple interfaces.
Q5: What are default methods in interfaces?
A: Default methods (Java 8+) are interface methods with implementation, marked with 'default'
keyword.
10. Applet Life Cycle
Q1: What are the main methods in the applet life cycle?
A: init(), start(), paint(), stop(), and destroy().
Q2: When is the init() method called?
A: init() is called once when the applet is first loaded.
Q3: What is the difference between start() and init()?
A: init() is called once during initialization, while start() is called each time the applet becomes
active.
Q4: When is the destroy() method called?
A: destroy() is called when the applet is about to be permanently removed from memory.
Q5: Why are applets less commonly used today?
A: Due to security concerns, plugin deprecation, and the rise of alternative web technologies like
HTML5 and JavaScript.
11. Division by Zero Exception
Q1: What exception is thrown when dividing by zero in Java?
A: ArithmeticException is thrown when dividing an integer by zero.
Q2: What happens if you divide a floating-point number by zero?
A: For floating-point numbers, it results in Infinity or -Infinity rather than an exception.
Q3: How can you prevent division by zero errors?
A: By checking if the denominator is zero before performing the division operation.
Q4: What is the difference between checked and unchecked exceptions?
A: Checked exceptions must be declared or handled, while unchecked exceptions (like
ArithmeticException) don't require explicit handling.
Q5: Can you create a custom exception for division by zero?
A: Yes, by extending the Exception or RuntimeException class to create a custom exception
class.
12. Method Overloading for Addition
Q1: What is method overloading in Java?
A: Method overloading is having multiple methods with the same name but different parameters
in the same class.
Q2: How does Java determine which overloaded method to call?
A: Based on the number, type, and order of arguments passed during the method call.
Q3: Can overloaded methods have different return types?
A: Yes, but return type alone is not sufficient to differentiate overloaded methods.
Q4: What are the advantages of method overloading?
A: It improves code readability, allows similar operations with different parameters, and
provides flexibility.
Q5: Can we overload main() method in Java?
A: Yes, the main() method can be overloaded, but only public static void main(String[] args) is
called by JVM.
13. Runtime Polymorphism
Q1: What is runtime polymorphism in Java?
A: Runtime polymorphism is when the method to be executed is determined at runtime based on
the actual object type.
Q2: How is runtime polymorphism achieved in Java?
A: Through method overriding where a subclass provides a specific implementation of a method
already defined in its parent class.
Q3: What is the difference between compile-time and runtime polymorphism?
A: Compile-time polymorphism (overloading) is resolved during compilation, while runtime
polymorphism (overriding) is resolved during execution.
Q4: What is the role of the JVM in runtime polymorphism?
A: JVM determines the actual object type at runtime and calls the appropriate overridden
method.
Q5: Can static methods exhibit runtime polymorphism?
A: No, static methods cannot be overridden (only hidden), so they don't exhibit runtime
polymorphism.
14. Negative Array Size Exception
Q1: What exception is thrown when creating an array with negative size?
A: NegativeArraySizeException is thrown.
Q2: How can you prevent NegativeArraySizeException?
A: By validating the array size before creating the array.
Q3: Is NegativeArraySizeException a checked or unchecked exception?
A: It's an unchecked exception (subclass of RuntimeException).
Q4: What is the parent class of NegativeArraySizeException?
A: It extends RuntimeException, which extends Exception.
Q5: Can you create an array with zero size in Java?
A: Yes, zero-length arrays are valid in Java.
15. Null Pointer Exception and Finally Block
Q1: What causes a NullPointerException in Java?
A: Trying to access or modify a null object's field, calling a method on null, or
accessing/indexing a null array.
Q2: What is the purpose of the finally block?
A: The finally block always executes, regardless of whether an exception occurs, used for
cleanup code.
Q3: Can a finally block be skipped in Java?
A: Only if the JVM exits (System.exit()) or the thread is interrupted before reaching the finally
block.
Q4: What happens if both catch and finally blocks throw exceptions?
A: The exception from the finally block is thrown, and the original exception is suppressed.
Q5: Can we have a try block without catch or finally?
A: No, a try block must be followed by either catch or finally or both.
16. User-Defined Packages
Q1: What is a package in Java?
A: A package is a namespace that organizes related classes and interfaces.
Q2: How do you create a user-defined package?
A: By using the package keyword as the first statement in a Java file.
Q3: What is the purpose of the import statement?
A: The import statement makes classes from other packages available without using fully
qualified names.
Q4: What is the difference between import and static import?
A: Regular import imports classes, while static import imports static members of a class.
Q5: How does Java locate user-defined packages?
A: Through the CLASSPATH environment variable or -classpath option.
17. Palindrome Number Check
Q1: What is a palindrome number?
A: A number that remains the same when its digits are reversed.
Q2: What are some common approaches to check for palindrome numbers?
A: Reverse the number and compare, or compare digits from both ends.
Q3: How would you handle negative numbers in palindrome check?
A: Negative numbers can't be palindromes because of the '-' sign.
Q4: What is the time complexity of a typical palindrome number check?
A: O(n) where n is the number of digits in the number.
Q5: Can you convert the number to a string to check for palindrome?
A: Yes, but it requires extra space for the string and is generally less efficient.
18. Factorial via Command Line Arguments
Q1: How do you access command line arguments in Java?
A: Through the String[] args parameter in the main method.
Q2: What data type are command line arguments received as?
A: They are received as Strings and need to be converted to numeric types.
Q3: How would you handle non-numeric command line arguments?
A: By catching NumberFormatException when parsing the arguments.
Q4: What is the maximum factorial that can be calculated using int/long?
A: 12! for int (overflow at 13!), 20! for long (overflow at 21!).
Q5: How would you calculate factorial for large numbers?
A: Using BigInteger class which can handle arbitrarily large numbers.
19. Prime Numbers Between Limits
Q1: What is a prime number?
A: A natural number greater than 1 that has no positive divisors other than 1 and itself.
Q2: What is the Sieve of Eratosthenes algorithm?
A: An efficient algorithm to find all primes up to a given limit by iteratively marking multiples
of primes.
Q3: How would you optimize checking for prime numbers?
A: Check divisibility only up to √n, skip even numbers after 2, etc.
Q4: What is the time complexity of checking if a number is prime?
A: O(√n) for the basic trial division method.
Q5: How would you handle the case when the lower limit is less than 2?
A: Either start from 2 or skip numbers less than 2 as they can't be prime.
20. Thread Using Runnable Interface
Q1: What are two ways to create threads in Java?
A: Extending Thread class or implementing Runnable interface.
Q2: Why is implementing Runnable generally preferred over extending Thread?
A: Because Java doesn't support multiple inheritance, and Runnable is more flexible.
Q3: What is the difference between start() and run() methods?
A: start() creates a new thread and calls run(), while calling run() directly executes it in the
current thread.
Q4: Can we call start() method twice on same thread object?
A: No, it throws IllegalThreadStateException as a thread can't be restarted.
Q5: What is the purpose of the join() method in threads?
A: join() makes the current thread wait until the called thread completes execution.