NAME:RYCHMUND ACHEAMPONG
INDEX NUMBER:IT24240076
COURSE TITLE: OOP WITH JAVA
LECTURER: [Link] NIKOI KOTEI
COURSE CODE:CSIS223
Core Java Basic Interview Questions and Answers
1) What are static blocks and static initializers in Java?
A static block is a block of code inside a class that runs once when the class is first
loaded into memory.
It is used to initialize static variables.
Example:
class Test {
static int num;
static {
num = 10;
[Link]("Static block executed");
2) How to call one constructor from the other constructor?
Using the keyword this().
Example:
class Test {
Test() {
this(5);
[Link]("Default Constructor");
Test(int x) {
[Link]("Parameterized Constructor with value " + x);
3) What is method overriding in Java?
When a subclass provides a specific implementation of a method already defined in the
superclass.
The method must have the same name, return type, and parameters.
Example:
class Parent {
void display() { [Link]("Parent display"); }
class Child extends Parent {
@Override
void display() { [Link]("Child display"); }
4) What is the super keyword in Java?
Refers to the immediate parent class object.
Used to:
1. Call parent class constructor.
2. Access parent class methods.
3. Access parent class variables.
5) Difference between method overloading and method overriding in Java?
Method Overloading vs Method Overriding:
| Feature | Method Overloading | Method Overriding |
|---------|--------------------|-------------------|
| Definition | Same method name with different parameter lists | Subclass redefines a
superclass method |
| Parameters | Must be different | Must be same |
| Return type | Can be different | Must be same or covariant |
| Compile/Run time | Resolved at compile-time | Resolved at runtime |
6) Difference between abstract class and interface?
Abstract Class vs Interface:
| Feature | Abstract Class | Interface |
|---------|---------------|-----------|
| Methods | Can have abstract & non-abstract methods | All methods are abstract (till Java
7); Java 8+ allows default/static |
| Variables | Can have instance and static variables | Only public static final (constants) |
| Inheritance | Single inheritance | Multiple inheritance possible |
| Usage | For “is-a” relationships | For “can-do” capabilities |
7) Why Java is platform-independent?
Java code is compiled into bytecode, not machine code.
Bytecode runs on the Java Virtual Machine (JVM), making it independent of the
underlying OS.
8) What is method overloading in Java?
Having multiple methods with the same name but different parameters (number or type).
Example:
class Math {
int add(int a, int b) { return a+b; }
double add(double a, double b) { return a+b; }
9) What is the difference between C++ and Java?
C++ vs Java:
| Feature | C++ | Java |
|---------|------|------|
| Platform | Platform-dependent | Platform-independent |
| Memory | Manual memory management | Automatic garbage collection |
| Multiple inheritance | Supported | Not directly supported (use interfaces) |
| Pointers | Supports pointers | No direct pointer support |
| Compilation | Compiles to machine code | Compiles to bytecode |
10) What is JIT compiler?
Just-In-Time (JIT) compiler improves performance by converting bytecode into native
machine code at runtime.
11) What is bytecode in Java?
An intermediate code generated after compiling Java code.
Runs on JVM, not tied to any OS.
12) Difference between this() and super() in Java?
this() vs super():
| Keyword | Usage |
|---------|-------|
| this() | Calls another constructor in the same class |
| super() | Calls parent class constructor |
13) What is a class?
A blueprint or template from which objects are created.
Example:
class Student { int id; String name; }
14) What is an object?
An instance of a class that contains states (fields) and behaviors (methods).
15) What is method in Java?
A block of code that performs a specific task and is executed when called.
16) What is encapsulation?
Wrapping data (variables) and methods together in a single unit (class).
Achieved using private variables + public getter/setter methods.
17) Why main() method is public, static, and void in Java?
public → accessible everywhere.
static → JVM can call it without creating an object.
void → does not return anything.
18) Explain about main() method in Java?
Entry point of any Java program.
Syntax:
public static void main(String[] args) {
// code
19) What is constructor in Java?
A special method with the same name as the class.
Used to initialize objects.
20) Difference between length and length() in Java?
length vs length():
| Property/Method | Usage |
|-----------------|-------|
| length | Used with arrays → [Link] |
| length() | Used with strings → [Link]() |
21) What is ASCII Code?
American Standard Code for Information Interchange.
Represents characters using numbers (0–127).
22) What is Unicode?
An extended character encoding standard that supports characters from all languages.
Java uses Unicode (16-bit).
23) Difference between Character Constant and String Constant in Java?
Character vs String Constant:
| Type | Example | Stored As |
|------|---------|-----------|
| Character Constant | 'A' | Single 16-bit Unicode value |
| String Constant | "Hello" | Sequence of characters (object in heap) |
24) What are constants and how to create constants in Java?
Constants are fixed values that do not change.
Created using the final keyword.
Example: final int MAX = 100;
25) Difference between >> and >>> operators in Java?
>> vs >>>:
| Operator | Meaning |
|----------|---------|
| >> | Signed right shift (preserves sign bit) |
| >>> | Unsigned right shift (fills with zero) |