Java makeup
23/08313
JACOBSON ETEMESI
1. Write a java application that prints, on separate lines, you name, your birthday, your
hobbies and your favourites books. Label each piece of information in the output.
(5marks)
INPUT
public class PersonalInfo {
public static void main(String[] args) {
String name = "Your Name"; // Replace with your actual name
String birthday = "YYYY-MM-DD"; // Replace with your actual birthday
String hobbies = "Reading, Coding, Hiking"; // Replace with your hobbies
String favoriteBooks = "Book1, Book2, Book3"; // Replace with your favorite
books
[Link]("Name: " + name);
[Link]("Birthday: " + birthday);
[Link]("Hobbies: " + hobbies);
[Link]("Favorite Books: " + favoriteBooks);
}
}
OUTPUT
Name: Your Name
Birthday: YYYY-MM-DD
Hobbies: Reading, Coding, Hiking
Favorite Books: Book1, Book2, Book3
=== Code Execution Successful ===
2. Explain the difference between the outputs from program statements in I and II below.
(2marks)
I. String s; [Link]("s = " + s);
The String variable s is declared but not initialized. When you try to print it, Java
will throw a compilation error because of trying to use a variable that has not been
given a value.
This results in a compilation error: "variable s might not have been initialized".
II. II. String s = new String(); [Link]("s = " + s);
The String variable s is explicitly initialized using new String(). This creates an
empty String object.
When you print it, it will output "s = " followed by an empty line, as the string
contains no characters.
3. Write a simple Java program to explain the working for do…while loop, write the
expected output of your program [3 Marks]
INPUT
public class DoWhileExample {
public static void main(String[] args) {
int i = 5;
[Link]("Starting the do-while loop:");
do {
[Link]("Current value of i: " + i);
i--; // Decrement i in each iteration
} while (i > 0); // Condition to continue the loop
[Link]("Loop finished.");
}
}
OUTPUT
Starting the do-while loop:
Current value of i: 5
Current value of i: 4
Current value of i: 3
Current value of i: 2
Current value of i: 1
Loop finished.
=== Code Execution Successful ===
4. With a well written Java Program explain the difference between method overloading
and method overriding [4Marks]
1 Method Overloading
class OverloadingExample {
// Method with one parameter
void display(int number) {
[Link]("Displaying an integer: " + number);
}
// Method with two parameters
void display(int number, String message) {
[Link]("Displaying an integer with a message: " + number + " - " +
message);
}
// Method with a different parameter type
void display(double value) {
[Link]("Displaying a double: " + value);
}
public static void main(String[] args) {
OverloadingExample example = new OverloadingExample();
[Link](10); // Calls method with one parameter
[Link](20, "Hello!"); // Calls method with two parameters
[Link](15.5); // Calls method with different parameter type
}
}
OUTPUT
Displaying an integer: 10
Displaying an integer with a message: 20 - Hello!
Displaying a double: 15.5
=== Code Execution Successful ===
Method Overriding INPUT
class Parent {
// Parent class method
void showMessage() {
[Link]("Message from Parent class");
}
}
class Child extends Parent {
// Child class overriding the parent's method
@Override
void showMessage() {
[Link]("Message from Child class");
}
}
public class OverridingExample {
public static void main(String[] args) {
Parent parentInstance = new Parent();
[Link](); // Calls Parent class method
Parent childInstance = new Child();
[Link](); // Calls Child class method (runtime polymorphism)
}
}
5. Define what you understand by JVM and explain its importance in Java Programming
[1 Marks]
The Java Virtual Machine (JVM) is a runtime environment that executes Java bytecode.
It acts as an intermediary between Java programs and the host system's hardware.
The JVM is the engine that drives Java's portability and robustness, making it a powerful
and versatile programming platform.
Importance of JVM:
Multithreading Support: Efficiently runs concurrent tasks
Security: Enforces strict security for safer code execution.