1) Reason primitive data-type usage.
| Primitive Type |
Description |
Size |
Example |
int |
Integer numbers |
4 bytes |
int a = 10; |
double |
Decimal numbers |
8 bytes |
double pi = 3.14; |
char |
Single character |
2 bytes |
char c = 'A'; |
boolean |
True/False |
1 bit* |
boolean flag = true; |
Wrapper classes?
| Primitive Type |
Wrapper Class |
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
JVM - Java Virtual Machine
JRE - Java Runtime Environment.
2) Example for static & dynamic memory allocation?
1. Static Memory Allocation
public class StaticExample {
static int x = 10; // Static variable (Method Area)
public static void main(String[] args) {
int a = 5; // Local variable (Stack)
System.out.println("a = " + a);
System.out.println("x = " + x);
}
}
Memory Allocated:
2. Dynamic Memory Allocation
public class DynamicExample {
public static void main(String[] args) {
String name = new String("Java"); // Object in Heap
int[] nums = new int[3]; 💗💗💗 // Array in Heap
nums[0] = 10;
System.out.println("Name = " + name);
System.out.println("First number = " + nums[0]);
}
}
Memory Allocated:
3) Array?
int[] numbers = new int[5]; // Declaration + memory for 5 integers
Operators?
1. Arithmetic Operators
| Operator |
Meaning |
Example |
Result |
+ |
Addition |
10 + 5 |
15 |
- |
Subtraction |
10 - 5 |
5 |
* |
Multiplication |
10 * 5 |
50 |
/ |
Division |
10 / 5 |
2 |
% |
Modulus |
10 % 3 |
1 |
2. Relational / Comparison Operators
| Operator |
Meaning |
Example |
Result |
== |
Equal to |
a == b |
false |
!= |
Not equal to |
a != b |
true |
> |
Greater than |
a > b |
false |
< |
Less than |
a < b |
true |
>= |
Greater or Equal |
a >= b |
false |
<= |
Less or Equal |
a <= b |
true |
3. Assignment Operators
| Operator |
Meaning |
Example |
Result |
= |
Assign |
a = 10 |
a = 10 |
+= |
Add and assign |
a += 5 |
a = a + 5 |
-= |
Subtract and assign |
a -= 3 |
a = a - 3 |
*= |
Multiply and assign |
a *= 2 |
a = a * 2 |
/= |
Divide and assign |
a /= 2 |
a = a / 2 |
4) What type of conditional statement is this?
if(10>=20) { "wrong" : " Ture"}
Java Ternary operator is used as one line
replacement for if-then-else statement
Syntax:
condition
? if true : if false;
Example 1:
public
class OperatorExample{
public static void main(String args[]){
int a=2;
int b=5;
int min=(a<b)? a : b; System.out.println(min);
}
}
5) Access Modifier?
1. private
Only accessible
within the same class.
class Test {
private int data = 100;
private void show() {
System.out.println("Private method");
}
}
2. default (No keyword)
Accessible only within the same package.
class Demo {
int number = 50; // default
void display() {
System.out.println("Default access");
}
}
3. protected
Accessible in the same package + subclasses (even in other packages).
// File: Animal.java
package animals;
public class Animal {
protected void sound() {
System.out.println("Animal makes sound");
}
}
// File: Dog.java
package animals;
public class Dog extends Animal {
void makeSound() {
sound(); // ✅ Accessible due to protected
}
}
4. public
Accessible everywhere.
// File: Message.java
package messages;
public class Message {
public void display() {
System.out.println("Hello World");
}
}
// File: Main.java
package test;
import messages.Message;
public class Main {
public static void main(String[] args) {
Message m = new Message();
m.display(); // ✅ Works everywhere
}
}
6) Constructor?
A constructor is a special method in Java that is used to initialize objects when they are created
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
}
7) OOP concepts?
1. Encapsulation
Wrapping data (variables) and methods into a single unit (class) and restricting direct access
class Student {
private String name; // hidden from outside
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
2. Inheritance
One class inherits properties and behaviors from another using extends
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
3. Polymorphism
One method behaves differently based on the object or context.
➤ Compile-time Polymorphism (Method Overloading)
class Add {
int sum(int a, int b) {
return a + b;
}
double sum(double a, double b) {
return a + b;
}
}
➤ Run-time Polymorphism (Method Overriding)
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Meow");
}
}
4. Abstraction
Hiding internal logic and showing only what is necessary.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
8) Difference between class and object?
A class is like a blueprint or design
1. Class
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
}
This class defines:
-
Properties: color
-
Behavior: drive()
2. Object
An object is an instance of a class. It’s like a real item based on the blueprint.
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Object created
myCar.color = "Red"; // Assigning value
myCar.drive(); // Calling method
}
}
9) Can we create more than 1 object for a same class?
Yes, we can create multiple objects more than 1
class Car {
String color;
void start() {
System.out.println(color + " car is starting");
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car(); // Object 1
car1.color = "Red";
Car car2 = new Car(); // Object 2
car2.color = "Blue";
car1.start(); // Output: Red car is starting
car2.start(); // Output: Blue car is starting
}
}
10) Types of inheritance?
1. Single Inheritance - One child inherits one parent.
2. Multilevel Inheritance - Grandchild inherits parent, parent inherits grandparent
3. Hierarchical Inheritance - Many children inherit one parent.
4. Multiple Inheritance - One class inherits from two interfaces.
5. Hybrid Inheritance - Combination of types, using interfaces
11) Polymorphism? Types of Polymorphism?
Compile time, Runtime
Abstract class? Abstract method?
abstract class Animal {
abstract void sound(); // only the name of the method
}
12) Difference between normal class and abstract class?
Normal class - Full class – you can use it directly.
Abstract Class - Half class – needs to be completed
13) for (int i = 0; i < 6; i++) {
int y = i;
System.out.println(y);
}
output -
0
1
2
3
4
5
14) for (int i = 0; i < 5; i++) {
int y = i++;
System.out.println(y); // Assuming "soul(y)" means "System.out.println(y);"
}
output:
Comments
Post a Comment