JAVA MODULE 1
BPLCK105C
1. Explain Object Oriented Principles
Ans :
a) Encapsulation
Definition:
Encapsulation is the process of hiding data and methods inside a
class to protect them from unintended modifications.
Syntax & Example:
class Car {
private String model;
public void setModel(String model) { this.model = model; }
public String getModel() { return model; }
}
b) Abstraction
Definition:
Abstraction is the concept of hiding complex implementation
details and exposing only essential features.
Syntax & Example:
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() { System.out.println("Car starts with a key."); }
}
c) Inheritance
Definition:
Inheritance allows a child class to acquire properties and behaviors
from a parent class.
Syntax & Example:
class Animal {
void makeSound() { System.out.println("Animal makes a
sound."); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks."); }
}
d) Polymorphism
Definition:
Polymorphism allows the same method to have different
behaviors depending on the context.
Syntax & Example:
class MathOperations {
int add(int a, int b) { return a + b; } // Overloading
double add(double a, double b) { return a + b; }
}
2.Write a Simple java program and explain
compilation process.
Ans :
Java compilation converts source code into bytecode, which is
executed by the Java Virtual Machine (JVM).
b) Compilation Steps:
Write Java code – Code is saved in a .java file.
Compile using javac – Generates a .class file containing
bytecode.
Example: javac Example.java
Execute using java – JVM runs the bytecode.
Example: java Example
c) Example:
class Example {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
d) Explanation of Compilation Process:
Source Code (Example.java) – The Java file contains human-
readable code.
Compilation (javac Example.java) – The Java compiler
converts .java code into bytecode (Example.class).
Execution (java Example) – The JVM reads bytecode, translates
it into machine code, and executes it
3. Describe the meaning of each of the keyword in “Public static void
main”
Ans :
a) Definition:
The main method is the starting point of every Java program.
b) Explanation of Keywords:
public – Accessible from anywhere.
a. Example: public void display() { }
static – Allows execution without creating an object.
b. Example: static void greet() { System.out.println("Hello!"); }
void – The method does not return a value.
c. Example: void print() { System.out.println("Print this!"); }
main – JVM looks for this method as the program entry point.
d. Example: public static void main(String[] args) { }
String[] args – Used to accept command-line arguments.
e. Example: System.out.println(args[0]);
4. Explain two control statements used in Java with
syntax and example.
Ans :
if Statement
Definition:
Executes a block of code only if the condition is true.
Syntax & Example:
if (x > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
b) Nested if Statement
Definition:
An if statement inside another if statement.
Syntax & Example:
if (age > 18) {
if (age < 60) {
System.out.println("Eligible for job");
}
}
c) for Loop
Definition:
A loop that executes a block of code a fixed number of times.
Syntax & Example:
for (int i = 1; i <= 5; i++) {
System.out.println("Iteration: " + i);
}
d) while Loop
Definition:
Executes a block of code as long as the condition is true.
Syntax & Example:
int i = 1;
while (i <= 5) {
System.out.println("Iteration: " + i);
i++;
}
5. Explain different lexical issues in JAVA
Ans :
a) Definition:
Lexical issues refer to how Java handles identifiers, literals, comments, separators, and
keywords.
b) Key Lexical Elements:
Whitespace – Java ignores spaces, tabs, and newlines except inside string literals.
Example:
int a = 10; // Valid with spaces and newlines
Identifiers – Names for variables, methods, and classes; must start with a letter or _.
Example:
int myVar; // Valid identifier
Literals – Fixed values such as numbers (100), characters ('A'), and strings ("Hello").
Example:
String name = "John"
Comments – Single-line (//), multi-line (/* */), and Javadoc (/** */).
Example:
// This is a single-line comment
/* Multi-line comment */
6. Explain the 8 primitive data types in Java.
Ans :
a) Definition:
Java has 8 primitive data types used to store numeric, character, and boolean values.
Table of Primitive Data Types:
Data Type Size Example
byte 8-bit byte a = 100;
short 16-bit short s = 32000;
int 32-bit int num = 10;
long 64-bit long l = 100000L;
float 32-bit float pi = 3.14f;
double 64-bit double d = 5.999;
char 16-bit char ch = 'A';
boolean isJavaFun =
boolean 1-bit true;
Explanation with Syntax & Example:
Integer (int) – Stores whole numbers.
o Syntax: int x = 100;
o Example:
int age = 25;
System.out.println("Age: " + age);
Floating-Point (float) – Stores decimal values.
Syntax: float num = 5.75f;
Example:
float price = 99.99f;
System.out.println("Price: " + price);
Boolean (boolean) – Stores true or false values.
Syntax: boolean isJavaEasy = true;
Example:
boolean isAdult = true;
System.out.println("Is Adult: " + isAdult);
7. Define a variable. Explain declaration and initialization of a
variable.
Ans :
a) Definition:
A variable is a storage location identified by a name, and initialization assigns a
value to it.
b) Types of Variable Declaration:
Explicit Initialization – Assigns a value at declaration.
Syntax: int x = 10;
Example:
int score = 95;
System.out.println("Score: " + score);
Dynamic Initialization – Uses an expression for initialization.
Syntax: dataType var = expression;
Example:
int a = 5, b = 10;
int sum = a + b;
System.out.println("Sum: " + sum);
Multiple Variable Declaration – Declares multiple variables of the same type.
Syntax: int a, b, c;
Example:
int x = 1, y = 2, z = 3;
System.out.println("Values: " + x + ", " + y + ", " + z);
8. Explain Scope and lifetime of variables with an example.
Ans :
a) Definition:
Scope defines where a variable is accessible, and lifetime determines how long it
exists.
b) Types of Scope:
Local Scope – Variable exists only inside a method or block.
Syntax:
void myMethod() {
int localVar = 10; // Only accessible inside this method
}
Example:
public class Example {
public static void main(String[] args) {
if (true) {
int x = 100; // x is local to this block
System.out.println("Value: " + x);
}
// x is not accessible here
}
}
Instance Scope – Variables belong to an object, accessible using this.
Syntax:
class MyClass { int instanceVar; }
Example:
class Car {
int speed; // Instance variable
}
Static Scope – Shared among all instances of a class.
Syntax:
static int count = 0;
Example:
class Counter {
static int count = 0; // Shared by all instances
}
9. What is type conversion and type casting? Explain with example.
(or)
Explain JAVA’s Automatic conversions and casting incompatible
type
Ans :
a) Type Conversion (Implicit)
Java automatically converts a smaller data type into a larger one when needed.
Type Conversion (Implicit/Widening)
Java automatically promotes smaller data types (byte, short, int) to larger ones
(long, float, double) without data loss.
Syntax & Example:
int num = 10;
double d = num; // Implicit conversion (int → double)
System.out.println("Double value: " + d);
Additional Points:
Widening conversion is safe because no data is lost.
Automatically applies in expressions when mixing smaller and larger types.
o Example:
float f = 10; // int to float
System.out.println("Float value: " + f);
Type Casting (Explicit/Narrowing)
Converting a larger type to a smaller type requires explicit casting, and
may cause data loss.
Syntax & Example:
double x = 9.8;
int y = (int) x; // Explicit casting (double → int)
System.out.println("Int value: " + y); // Output: 9 (decimal part lost)
10. Explain one dimensional and two dimensional array with example for
each
Ans :
One-Dimensional Array
Definition:
A one-dimensional array is a collection of elements of the same data type, stored in contiguous
memory locations and accessed using an index.
Syntax:
type arrayName[] = new type[size];
Example:
class OneDArray {
public static void main(String args[]) {
int month_days[] = new int[12]; // Declare and allocate memory
month_days[0] = 31; // Assign values
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
System.out.println("February has " + month_days[1] + " days.");
}
}
Two-Dimensional Array
Definition:
A two-dimensional array is an array of arrays, where each row represents a separate array.
Syntax:
type arrayName[][] = new type[rows][columns];
Example :
class TwoDArray {
public static void main(String args[]) {
int twoD[][] = new int[4][5]; // Declare a 4x5 2D array
int i, j, k = 0;
// Assign values
for(i = 0; i < 4; i++)
for(j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
// Display the array
for(i = 0; i < 4; i++) {
for(j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}