MOD 1 and 2
MOD 1 and 2
Here are concise 2-marks answers to your questions based on standard study material:
Procedural Programming is based on functions and procedures, while OOP is based on objects and
classes.
OOP supports encapsulation, inheritance, and polymorphism; Procedural does not.
Java provides strong memory management, exception handling, and type checking at compile-time
and runtime.
Java is called architecturally neutral because it runs on any platform with the help of the JVM,
regardless of the underlying architecture.
Bytecode is an intermediate code generated by the Java compiler which is executed by the JVM.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/16
[5] Give any four differences between C and Java.
Java combines features of both compiled and interpreted languages, using a compiler to generate
bytecode and the JVM to interpret it.
[7] Draw the relationship among JVM, JRE & JDK using Venn diagram.
(Use this description to draw a Venn diagram):
Java bytecode can run on any device with a JVM, making it platform-independent.
It must be public so the JVM can access and execute it from outside the class.
[10] Explain each term of the following: `public static void main(String [] args)`.
JDK (Java Development Kit) is a software development kit that includes tools like compiler (`javac`),
JRE, and debugger for developing Java applications.
Download JDK from Oracle’s website → Run installer → Set environment variables (`JAVA_HOME`,
`Path`) → Verify using `java -version`.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/16
Unicode is a character encoding standard that represents characters from all languages.
JIT (Just-In-Time) compiler improves performance by compiling bytecode into native machine code
at runtime.
So it can be executed without creating an object of the class, enabling program startup.
JRE (Java Runtime Environment) provides the libraries and JVM needed to run Java applications.
JVM (Java Virtual Machine) executes Java bytecode and provides platform independence.
Through bytecode optimization and the use of the JIT compiler which converts bytecode to efficient
native code at runtime.
Java and JavaScript are unrelated in functionality; Java is a programming language, while JavaScript
is a scripting language used mainly in web development. They share similar names due to
marketing strategies.
Here are well-structured 5-mark and 10-mark answers based on common academic study material:
✅ 5 Marks Questions
[1] Develop a diagram illustrating the process of compiling and interpreting Java code,
highlighting the role of the Just-In-Time Compiler (JIT) and its significance in this process.
Diagram Description:
less
The Just-In-Time (JIT) compiler improves runtime performance by compiling bytecode to native
machine code.
It works alongside the interpreter: frequently used code is compiled once and reused, speeding
up execution.
Everything in Java is an object (except primitive types, which are handled via wrapper
classes).
Supports core OOP principles: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/16
Feature C++ Java
4. Secure – Java web apps use security features like bytecode verification and access control.
5. Multithreaded – A chat application where messages, typing indicators, and notifications run on
separate threads.
[5] What are the different components in the Java environment and their usage?
1. JDK (Java Development Kit) – Used to develop and compile Java applications.
2. JRE (Java Runtime Environment) – Runs Java programs.
3. JVM (Java Virtual Machine) – Converts bytecode into machine code at runtime.
4. Java API – Provides a rich set of libraries and tools.
5. Development Tools – Includes compiler (`javac`), debugger (`jdb`), and documentation tool
(`javadoc`).
[6] What are the advantages of a platform-independent language? Also explain how Java is
platform independent.
Advantages:
✅ 10 Marks Questions
[1] Sketch and demonstrate the architecture of JVM.
JVM Architecture Components:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/16
scss
Explanation:
[2] What are the elements of OOP? How do these elements make the OOP approach?
Elements of OOP:
1. Encapsulation – Bundles data and methods (e.g., class with private variables).
2. Inheritance – Reuse code from parent class (e.g., Car extends Vehicle).
Encapsulation
Abstraction
Inheritance
Polymorphism
Modularity
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/16
Reusability
Benefits:
Real-world Modeling – Objects reflect real entities, making system design intuitive.
Let me know if you'd like diagrams, formatted notes, or this content in a PDF format!
2 Marks Questions
REFERENCE: STUDY MATRERIAL
[1] Express a Java programming structure to display “Hello World”.
[2] Write down the signifiance of static keyword.
[3] What are the benifits of reuseability properties?
[4] What happens if the constructors are defined as private?
[5] Write a program to print a string in reverse order.
[6] Write a program to show the use of enhanced for loop in java.
[7] What is a constructor? Explain with suitable example.
[8] Distinguish between instance variables and static variables with a code snippet.
[9] String is immutable in java - justify your answer with a code snippet.
[10] Can you call one constructor from another if a class has multiple constructors?
[11] How can a subclass call a method or a constructor defined in a superclass?
[12] What is the basic difference between string and stringbuffer objects?
[13] How objects of a class are created if no constructor is defined in the class?
[14] Write down the difference between Concat method and + operator to join strings?
[15] Explain default method in java.
[16] What are the relationships between classes?
[17] What is a break statement?
[18] What is a continue statement?
[19] What is a “blank final” variable?
[20] What is the precedence of an operator?
[21] What is the Associavity of an operator?
Here are the answers to the 2-marks questions based on typical Java study material:
✅ 2 Marks Questions
[1] Express a Java programming structure to display “Hello World”.
java
The `static` keyword denotes that a member (variable or method) belongs to the class rather than
instances.
java
[6] Write a program to show the use of enhanced for loop in Java.
java
A constructor is a special method used to initialize objects. It has the same name as the class and
no return type.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/16
java
[8] Distinguish between instance variables and static variables with a code snippet.
java
[9] String is immutable in Java - justify your answer with a code snippet.
java
[10] Can you call one constructor from another if a class has multiple constructors?
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/16
[12] What is the basic difference between String and StringBuffer objects?
[13] How objects of a class are created if no constructor is defined in the class?
[14] Write down the difference between `concat` method and `+` operator to join strings.
Introduced in Java 8, default methods allow interfaces to have method implementations using
`default` keyword.
Inheritance (is-a)
Association (has-a)
Aggregation / Composition (whole-part)
Used to skip current iteration and continue with the next one in loops.
A final variable that is not initialized at declaration but must be initialized in the constructor.
Let me know if you want this in a PDF or need any questions explained further!
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/16
5 Marks Questions
REFERENCE: STUDY MATRERIAL
[1] Develop a Java application to generate Electricity bills. Create a class with the following members:
Consumer no., consumer name, previous month reading, current month reading, and type of EB
connection (i.e domestic or commercial). Compute the bill amount using the following tariff. If the
type of the EB connection is domestic, calculate the amount to be paid as follows:
First 100 units - Rs. 1 per unit
101-200 units - Rs. 2.50 per unit
201 -500 units - Rs. 4 per unit
> 501 units - Rs. 6 per unit
[2] Write a program to show the use of object as a return type and parameter.
[3] Given a string, return a new string made of 'n' copies of the last 2 chars of the original string
where 'n' is the length of the string. Input:Coding Output: ngngngngngng
[4] Given a string, return a new string made of 'n' copies of the first 2 chars of the original string
where 'n' is the length of the string. Input:Hello Output: HeHeHeHeHe
[5] Write about the operators in JAVA with examples.
[6] How is the for each loop different from the for loop?
[7] Using up to four characters, write the Java representation of the octal literal 6.
[8] How many bits represent a Boolean variable? Why?
[9] How can we make a class immutable?
[10] Discus constructing a string with character array and String class.
10 Marks Questions
REFERENCE: STUDY MATRERIAL
[1] Discuss different access specifier of java. Explain each with examples.
[2] a) Explain public static void main(String args[]) in Java. b) Explain each token in the statement:
System.out.println(“Hello Java”);
[3] Write a program that reads ten numbers through command line argument, computes their
average, and finds out how many numbers are above the average.
[4] Explain different iterators used for accessing the elements,with examples.
[5] Discus operators and control statements in Java in detail.
[6] Write a program that defines a class that represents a complex number.Write a method in the
class that returns the magnitude or modulus of the complex number on which it is called.
Here are the well-structured answers for the 5-marks and 10-marks questions from your study
material:
✅ 5 Marks Questions
[1] Java Program to Generate Electricity Bill (Domestic)
java
`class ElectricityBill {
int consumerNo;
String consumerName;
int prevReading, currReading;
String connectionType;
ElectricityBill(int no, String name, int prev, int curr, String type) {
consumerNo = no;
consumerName = name;
prevReading = prev;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/16
currReading = curr;
connectionType = type;
}
double calculateBill() {
int units = currReading - prevReading;
double amount = 0;
if (connectionType.equalsIgnoreCase("domestic")) {
if (units <= 100) amount = units * 1;
else if (units <= 200) amount = 100 * 1 + (units - 100) * 2.5;
else if (units <= 500) amount = 100 * 1 + 100 * 2.5 + (units - 200) * 4;
else amount = 100 * 1 + 100 * 2.5 + 300 * 4 + (units - 500) * 6;
}
return amount;
}
java
`class Box {
int length, breadth;
Box(int l, int b) {
length = l;
breadth = b;
}
Box biggerBox(Box b) {
if (length * breadth > b.length * b.breadth) return this;
else return b;
}
void display() {
System.out.println("Area: " + (length * breadth));
}
java
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/16
}
`
java
java
`int a = 10, b = 5;
System.out.println(a + b); // 15
`
java
Boolean values in Java use 1 bit conceptually but occupy 1 byte (8 bits) in memory for alignment
and compatibility.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/16
[9] Making a Class Immutable
Steps:
java
java
✅ 10 Marks Questions
[1] Access Specifiers in Java
protected ✓ ✓ ✓ ✗
public ✓ ✓ ✓ ✓
Examples:
java
`public class A {
private int x;
protected int y;
public int z;
}
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 14/16
[2a] Explain `public static void main(String args[])`
java
Example:
java
`import java.util.*;
public class IteratorDemo {
public static void main(String[] args) {
List<String> list = List.of("A", "B", "C");
Iterator<String> it = list.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/16
}
`
java
`class Complex {
double real, imag;
Complex(double r, double i) {
real = r;
imag = i;
}
double magnitude() {
return Math.sqrt(real * real + imag * imag);
}
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/16