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

Java Question Bank Unit 1 (1)

The document is a comprehensive Java question bank covering key concepts of Java programming, including object-oriented programming principles, Java architecture, data types, variable scope, and differences between Java and C++. It also discusses operators, control statements, and the benefits of object-oriented programming, along with examples and explanations for various Java features. Additionally, it highlights the differences between Java and C++, procedural vs. object-oriented development, and provides insights into Java data types.

Uploaded by

Inika Dey
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 views10 pages

Java Question Bank Unit 1 (1)

The document is a comprehensive Java question bank covering key concepts of Java programming, including object-oriented programming principles, Java architecture, data types, variable scope, and differences between Java and C++. It also discusses operators, control statements, and the benefits of object-oriented programming, along with examples and explanations for various Java features. Additionally, it highlights the differences between Java and C++, procedural vs. object-oriented development, and provides insights into Java data types.

Uploaded by

Inika Dey
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
You are on page 1/ 10

JAVA QUESTION BANK

Q1. Explain the concept of object-oriented programming in Java. Give an example.

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of


"objects", which can contain data (fields/attributes) and code (methods).

Key OOP Principles in Java:

1. Encapsulation – Bundling data and methods into a single unit (class).


2. Inheritance – One class can inherit properties and methods from another.
3. Polymorphism – One method behaves differently based on the object (method
overloading/overriding).
4. Abstraction – Hiding complex details and showing only essential features.

Example:

class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void makeSound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal a = new Dog(); // Polymorphism
a.makeSound(); // Output: Dog barks
}
}

This example demonstrates inheritance and polymorphism.

Q2. What is Java Architecture? Explain its components.

Java Architecture refers to the structure that allows Java code to be written once and run anywhere
(platform independence).

Key Components:

1. Java Source Code (.java) – The code you write.


2. Java Compiler (javac) – Converts .java files into bytecode (.class files).
3. Bytecode – Intermediate, platform-independent code.
4. Java Virtual Machine (JVM) – Executes bytecode on any machine.
5. Java Runtime Environment (JRE) – Contains JVM + libraries needed to run Java applications.
6. Java Development Kit (JDK) – Includes JRE + tools (compiler, debugger, etc.) for
development.

Flow:

Source Code (.java) → Compiled by javac → Bytecode (.class) → Executed by JVM

Q3. What is the difference between data types and literals in Java?

Data Types Literals


Describe the kind of data a variable can hold. Actual values assigned to variables.
Examples: int, float, char, boolean, etc. Examples: 10, 3.14, 'A', "Hello"
Defines memory size and operations allowed. Represent constant values used in code.

Example:

int number = 10;

 int → data type


 10 → literal

Q4. Explain the scope and lifetime of variables in Java.

Scope: Where a variable can be accessed in the code.

Types of Scope in Java:

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:

 Developed by James Gosling and his team at Sun Microsystems in 1995.


 Initially named Oak, later renamed to Java.
 Designed to be platform-independent for embedded systems and web applications.
 Now owned by Oracle Corporation.

Key Features of Java:

1. Object-Oriented – Everything is based on classes and objects.


2. Platform Independent – Write Once, Run Anywhere (thanks to JVM).
3. Simple – Easy to learn and use.
4. Secure – Provides a secure execution environment (no pointer, access control).
5. Robust – Strong memory management and exception handling.
6. Multithreaded – Supports multiple threads for parallel execution.
7. High Performance – Bytecode runs fast due to JIT (Just-In-Time) compiler.
8. Distributed – Supports networking and remote method invocation (RMI).
9. Portable – Code is architecture-neutral and runs on any OS.

Q6. Differentiate between Java and C++. Explain the architecture of Java.

Difference between Java and C++:

Feature Java C++


Platform Platform-dependent (compiled to
Platform-independent (uses JVM)
Dependency machine code)
Memory
Automatic (Garbage Collection) Manual (using delete)
Management
Not supported directly (uses
Multiple Inheritance Supported using classes
interfaces)
Pointers Not allowed (for security) Allowed
Compiled to bytecode, then
Compilation Directly compiled to machine code
interpreted
GUI Support Built-in with libraries like Swing Uses external libraries like Qt
Uses header files and more complex
Code Simplicity Simpler syntax, no header files
syntax

Java Architecture (Revised Explanation):

1. Source Code (.java)


o Written by the programmer.
2. Compiler (javac)
o Converts .java file into bytecode (.class).
3. Bytecode
o Platform-independent intermediate code.
4. Java Virtual Machine (JVM)
o Reads and executes bytecode on any platform.
o Handles memory, garbage collection, security, etc.
5. JRE (Java Runtime Environment)
o Contains JVM + class libraries for running Java programs.
6. JDK (Java Development Kit)
o Contains JRE + development tools (javac, debugger, etc.).

Q7. What are the different types of operators in Java? Give examples of each type.

Java supports several types of operators:

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");
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car(); // Object created
myCar.color = "Red";
myCar.drive();
}
}

 Car → class
 myCar → object of class Car

Q9. Write a Java program to demonstrate the use of arrays.

public class ArrayExample {


public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

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

Q10. Explain the difference between JDK, JVM, and JRE.

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:

 JDK = Kitchen (for cooking and running food)


 JRE = Dining Table (for only consuming food)
 JVM = Cook (who prepares the bytecode dish to run)

Q11. What is a variable? How to declare variable in Java?

Variable:

 A container (name) used to store data in a program.


 Each variable has a data type, name, and optionally an initial value.

Declaration Syntax:

dataType variableName;

Declaration with Initialization:

int age = 20;


String name = "John";

Example:

public class Example {


public static void main(String[] args) {
int number = 5; // integer variable
float price = 10.5f; // float variable
char grade = 'A'; // character variable
boolean pass = true; // boolean variable
}
}

Q12. What is a variable? What are the different types of variables?

✅ Variable:
A variable is a named memory location that stores data which can change during program execution.

✅ Types of Variables in Java:

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++;
}
}

Q14. Write a note on conditional operator in Java.

✅ Conditional (Ternary) Operator:


A shorthand version of if-else condition.

Syntax:

condition ? value_if_true : value_if_false;

Example:

int a = 10, b = 20;


int max = (a > b) ? a : b; // max = 20

Q15. List out the operators in Java with 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

Q16. Explain the Control Statements in Java with example.

✅ Control Statements manage program flow. Types include:

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. }

Q17. Difference between JAVA and C++.

Feature Java C++


Platform Platform-independent (JVM) Platform-dependent
Memory Management Automatic (Garbage Collector) Manual (delete)
Multiple Inheritance Not supported (uses interfaces) Supported via classes
Security More secure Less secure due to pointers
Compilation Compiles to bytecode Compiles to native machine code

Q18. Difference between Procedural and Object-Oriented Development

Feature Procedural Programming Object-Oriented Programming (OOP)


Focus Functions/Procedures Objects and Classes
Data Handling Global data; less secure Encapsulation; data hidden in objects
Code Reusability Limited High via inheritance
Example Languages C, Pascal Java, C++, Python

Q19. Write short note on:

1. Java Token:
 Smallest unit in a Java program.
 Types: Keywords, Identifiers, Literals, Operators, Separators, Comments.

2. For Loop:

 Used to repeat a block of code known number of times.

for (int i = 0; i < 5; i++) {


System.out.println(i);
}

3. While and Do-While Loop:

 while: checks condition first.

int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}

 do-while: executes at least once.

int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);

Q20. Discuss Applications and Benefits of OOP.

✅ Applications:

 GUI applications (Swing, JavaFX)


 Mobile apps (Android)
 Web-based apps (Servlets, JSP)
 Game development
 Enterprise applications (banking, billing systems)

✅ Benefits of OOP:

1. Modularity – Code is divided into classes.


2. Reusability – Inheritance allows reuse.
3. Security – Data hiding using encapsulation.
4. Flexibility – Polymorphism makes code more flexible.
5. Maintenance – Easier to manage and update.

Q21. Explain Datatypes in Java with example.


✅ Java Data Types are divided into:

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:

 Arrays, Strings, Classes, Objects

String name = "John";


int[] arr = {1, 2, 3};

You might also like