0% found this document useful (0 votes)
2 views2 pages

Java Assignment Questions

Uploaded by

aniissiciid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Assignment Questions

Uploaded by

aniissiciid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

OBJECT ORIENTED PROGRAMMING || in Java

Assignment questions. (20 Marks).

Part A: Conceptual & Short Answer Questions

1. Java Fundamentals & Data Types

Q1. Explain the concept of “Write Once, Run Anywhere” (WORA) in Java.
What role does the Java Virtual Machine (JVM) play in this?

Q2. Differentiate between **primitive data types** and **reference data


types** in Java. Provide two examples of each.

2. Operators & Control Flow

Q3. Consider the expression boolean result = (5 > 3) && (10 / 0 == 0);.
Explain why this code does or does not throw an ArithmeticException at
runtime, referencing the concept of **short-circuit evaluation.

Q4. Compare and contrast a while loop and a for loop. When would you
strategically choose one over the other?

3. Object-Oriented Programming (OOP) Principles

Q5. Define **Encapsulation** and explain how it is achieved in Java using


access modifiers (private, public) and getter/setter methods.

Q6. What is the core difference between an **Abstract Class** and an


**Interface** in Java? When should you use an interface instead of an
abstract class?

4. Memory & Data Structures

Q7. How do **Arrays** differ from **ArrayLists** in Java regarding


memory allocation and resizing flexibility?

Q8. Explain how a HashMap stores and retrieves data. What is the
significance of keys and values?

Part B: Practical Coding Challenges

5. Operators & Control Flow

Q9. Write a Java program that prompts the user to enter an integer. The
program should:

* Check if the number is even or odd using the modulus operator (%).

* Print a message stating whether the number is prime or composite


using a for or while loop.

6. Arrays & Methods


Q10. Create a Java method named findLargestAndSmallest(int[] numbers)
that accepts an array of integers. The method should find and print both
the largest and the smallest numbers in the array.

> *Bonus:* Ensure your method handles empty arrays gracefully


without crashing.

>

7. OOP Implementation

Q11. Design a simple system using the following requirements:

* Create an abstract class named Vehicle with an abstract method


startEngine().

* Create a class named Car that inherits from Vehicle. Add a private
property fuelLevel, implement encapsulation, and override the
startEngine() method to check if there is enough fuel before starting.

8. Strings & Exception Handling

Q12. Write a program that takes a string input from a user and reverses
it.

Q13. Wrap your user input logic from **Q12** in a try-catch-finally block.
Specifically handle a NullPointerException if a null string is passed, and
use the finally block to print a message saying “String processing
complete.”

9. Collections & File Handling

Q14. Write a Java program that reads a list of student names from a text
file named [Link].

* Store these names into an ArrayList<String>.

* Sort the list alphabetically.

* Write the sorted list out into a new file named sorted_students.txt.

You might also like