Java Question Bank Unit 1 (1)
Java Question Bank Unit 1 (1)
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
Java Architecture refers to the structure that allows Java code to be written once and run anywhere
(platform independence).
Key Components:
Flow:
Q3. What is the difference between data types and literals in Java?
Example:
1. Local Variables:
o Declared inside a method/block.
o Accessible only within that method/block.
o Not initialized by default.
o Lifetime: Only during method execution.
2. Instance Variables:
o Declared in a class, outside methods.
o Each object gets its own copy.
o Initialized to default values if not assigned.
o Lifetime: Exists as long as the object exists.
3. Static (Class) Variables:
o Declared with static keyword.
o Shared among all instances of the class.
o Lifetime: Exists for the life of the program.
Q5. Explain the history and features of Java as an object-oriented programming language.
History of Java:
Q6. Differentiate between Java and C++. Explain the architecture of Java.
Q7. What are the different types of operators in Java? Give examples of each type.
1. Arithmetic Operators
o +, -, *, /, %
o Example: int sum = a + b;
2. Relational (Comparison) Operators
o ==, !=, >, <, >=, <=
o Example: if (a > b)
3. Logical Operators
o &&, ||, !
o Example: if (a > 5 && b < 10)
4. Assignment Operators
o =, +=, -=, *=, /=, %=
o Example: x += 5;
5. Increment/Decrement Operators
o ++, --
o Example: i++; or --j;
6. Bitwise Operators
o &, |, ^, ~, <<, >>, >>>
o Example: a & b, a << 2
7. Ternary Operator
o condition ? value_if_true : value_if_false
o Example: int max = (a > b) ? a : b;
8. Instanceof Operator
o Checks if an object is an instance of a class.
o Example: if (obj instanceof String)
Q8. What is the difference between a class and an object in Java? Explain with example.
Class Object
Blueprint or template for creating objects Instance of a class
Does not occupy memory Occupies memory
Defines properties and behaviors Performs actions and holds values
Example:
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
}
Car → class
myCar → object of class Car
System.out.println("Array elements:");
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
Output:
Array elements:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Component Description
JDK (Java Development Full development kit for Java; includes JRE + development tools like
Kit) compiler (javac), debugger, etc.
JRE (Java Runtime
Environment to run Java programs; includes JVM + core libraries.
Environment)
JVM (Java Virtual Part of JRE that executes Java bytecode; provides platform
Machine) independence.
Analogy:
Variable:
Declaration Syntax:
dataType variableName;
Example:
✅ Variable:
A variable is a named memory location that stores data which can change during program execution.
1. Local Variable: Declared inside a method or block; accessible only within that scope.
2. Instance Variable: Non-static variable declared inside a class but outside methods; belongs
to each object.
3. Static Variable: Declared using static; shared among all instances of a class (class-level
variable).
Q13. What is the difference between static variable and instance variable?
Static Variable Instance Variable
Shared among all objects of a class Unique to each object
Declared with static keyword No static keyword
Memory allocated once at class loading Memory allocated when object is created
Can be accessed using class name Accessed using object name
Example:
class Test {
static int count = 0; // static
int id; // instance
Test(int id) {
this.id = id;
count++;
}
}
Syntax:
Example:
✅ Types of Operators:
1. Arithmetic: +, -, *, /, %
👉 int sum = a + b;
2. Relational: ==, !=, <, >, <=, >=
👉 if (a > b)
3. Logical: &&, ||, !
👉 if (a > 0 && b < 10)
4. Assignment: =, +=, -=, *=, /=
👉 x += 5;
5. Unary: ++, --, +, -, !
👉 a++, --b, !flag
6. Bitwise: &, |, ^, <<, >>
👉a&b
7. Ternary: condition ? val1 : val2
👉 int max = (a > b) ? a : b;
8. instanceof: Checks type
👉 obj instanceof String
1. Decision Making:
o if, if-else, switch
2. if (a > b) System.out.println("a is greater");
3. Looping:
o for, while, do-while
4. for (int i = 0; i < 5; i++) System.out.println(i);
5. Jumping:
o break, continue, return
6. for (int i = 0; i < 5; i++) {
7. if (i == 3) break;
8. }
1. Java Token:
Smallest unit in a Java program.
Types: Keywords, Identifiers, Literals, Operators, Separators, Comments.
2. For Loop:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
✅ Applications:
✅ Benefits of OOP:
1. Primitive Types:
| Type | Size | Example |
|----------|----------|-------------------|
| byte | 1 byte | byte b = 10; |
| short | 2 bytes | short s = 200; |
| int | 4 bytes | int a = 1000; |
| long | 8 bytes | long l = 100000L;|
| float | 4 bytes | float f = 10.5f; |
| double | 8 bytes | double d = 99.99;|
| char | 2 bytes | char c = 'A'; |
| boolean| 1 bit | boolean b = true;|
2. Non-Primitive (Reference) Types: