Assignment questions(all essay questions)
1. (a) Explain the core principles of Object-Oriented Programming (Encapsulation,
Inheritance, Polymorphism, Abstraction) with a suitable real-world analogy for each
principle. (5 Marks)
(b) Draw a neat diagram illustrating the Java execution process from a `.java` source code
file to its execution on a machine. Clearly label JDK, JRE, JVM, `javac` (Compiler), and
Bytecode in the flow. (5 Marks)
2. (a) What is Type Casting in Java? Explain the difference between Implicit (Widening)
Casting and Explicit (Narrowing) Casting with clear code examples for each. (5 Marks)
(b) Write a complete Java program using a `for` loop that prints the multiplication table for
a number provided by the user. The program should use the `Scanner` class to get input. (5
Marks)
3. (a) Explain the `static` keyword in detail as it applies to variables and methods. What is
the key difference between a `static` member and an instance member? (5 Marks)
(b) Write a complete Java program with a `Counter` class. This class should have a `static`
variable `instanceCount` that is incremented in the constructor. In your `main` method, create
three objects of the `Counter` class and then print the final value of `instanceCount` to
demonstrate that it is shared among all objects. (5 Marks)
4. (a) What is a variable in Java? Explain the difference between declaration, initialization,
and definition of a variable with a code example for each. (5 Marks)
(b) Write a complete Java program that takes the temperature in Celsius from the user as
input (using the `Scanner` class) and converts it to Fahrenheit. *(Formula: F = (C * 9/5) +
32)*. (5 Marks)
2. (a) Explain the `for` loop with its syntax and a flowchart. Describe a situation where a
`for` loop is more appropriate than a `while` loop. (5 Marks)
(b) Write a complete Java program to check if a number entered by the user is a prime
number. The program should use a loop and conditional statements. (5 Marks)
3. (a) What are constructors and why are they needed for a class? Explain the difference
between a default constructor and a parameterized constructor with a suitable code example.
(5 Marks)
(b) Write a Java program to demonstrate method overloading. Create a `Calculator` class
with at least three overloaded `add()` methods that can perform addition for:
i. Two integers
ii. Three integers
iii. Two double values
(5 Marks)
4. (a) Explain the concept of encapsulation and data hiding in Java. How are `private`
access modifiers and getter/setter methods used to achieve this? Provide a simple example. (5
Marks)
(b) Refactor the following incomplete `Product` class to be properly encapsulated. Add a
parameterized constructor and a method `displayProductDetails()` to print the product
information.
class Product {
public int productId;
public String productName;
public double price;
}
```
(5 Marks)
5. (a) Explain the `if-else-if` ladder control flow statement with its syntax and a flowchart.
Describe a scenario where it is more suitable than a simple `if-else`. (5 Marks)
(b) Write a complete Java program that prompts the user for their exam score (an integer
between 0 and 100). The program must then use an `if-else-if` ladder to determine and print
the corresponding grade based on the following rules:
* 90 - 100: Grade A
* 80 - 89: Grade B
* 70 - 79: Grade C
* 60 - 69: Grade D
* Below 60: Grade F
(5 Marks)
6. (a) Explain the `static` keyword in detail as it applies to variables and methods. What is
the key difference between a `static` variable and an instance variable? (5 Marks)
(b) Write a complete Java program with a `Car` class. The class should have a `private`
instance variable `speed` and `public` methods `accelerate()` (which increases speed) and
`brake()` (which decreases speed). Also, include a `getSpeed()` method. Demonstrate
creating a `Car` object and calling these methods in `main`. (5 Marks)
7. (a) What is the `this` keyword in Java? Demonstrate its two primary uses (resolving
variable name ambiguity in constructors/setters and invoking another constructor from a
constructor) with a clear code example. (5 Marks)
(b) Write a Java program to demonstrate constructor overloading. Create a `Laptop` class
with properties `brand` and `ramSize`. Provide three constructors:
i. A no-argument constructor that sets default values.
ii. A constructor that accepts only the `brand`.
iii. A constructor that accepts both `brand` and `ramSize`.
(5 Marks)
8. (a) What are relational and logical operators in Java? List them and explain the purpose of
`&&` (AND), `||` (OR), and `!` (NOT) with a truth table or examples. (5 Marks)
(b) Write a complete Java program that defines a class `Rectangle`. The class should have
two `private` fields, `length` and `width`. Include a parameterized constructor and a method
`calculateArea()` that returns the area. Create and use a `Rectangle` object in the `main`
method. (5 Marks)
9. (a) Explain the `switch` statement with its syntax. What is the purpose of the `break` and
`default` keywords within a switch block? (5 Marks)
(b) Write a complete Java program that acts as a simple calculator. It should take two
numbers and an operator (`+`, `-`, `*`, `/`) as input from the user and then use a `switch`
statement to perform the correct operation and display the result. (5 Marks)