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

Java Mid Term Questions

The document contains a series of midterm questions focused on key concepts of Object-Oriented Programming (OOP) in Java, including encapsulation, abstraction, inheritance, and various programming constructs. It includes tasks such as writing code snippets for specific classes, explaining Java methods, and demonstrating loops and data types. The questions aim to assess understanding of Java fundamentals and application of OOP principles.

Uploaded by

mohitit294
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 Mid Term Questions

The document contains a series of midterm questions focused on key concepts of Object-Oriented Programming (OOP) in Java, including encapsulation, abstraction, inheritance, and various programming constructs. It includes tasks such as writing code snippets for specific classes, explaining Java methods, and demonstrating loops and data types. The questions aim to assess understanding of Java fundamentals and application of OOP principles.

Uploaded by

mohitit294
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

MidTerm questions

1. What is the primary goal of encapsulation in OOP, and how is it achieved in Java using access
modifiers?

2. Write a Java code snippet demonstrating encapsulation with a private variable and public
getter/setter methods for a "Book" class.

3. How does abstraction differ from encapsulation in OOP? Provide a Java example showcasing
abstraction using an interface.

4. Explain inheritance in OOP. Write a Java program where a "Dog" class inherits from an
"Animal" superclass.

5. Define a class and an object in Java. Create a "Laptop" class with two attributes and instantiate
an object of it.

6. Describe the role of the `main` method in a Java program. Write the basic structure of a Java
program with a `main` method.

7. What are the steps to compile and run a Java program using the command line? Explain the
roles of `javac` and `java` commands.

8. Differentiate between constants and literals in Java. Provide examples of a string literal and a
final constant.

9. What are identifiers in Java? List three rules for naming identifiers and give two valid
examples.

10. Name all primitive data types in Java and explain the difference between `int` and `double`
with their memory sizes.

11. Write a Java program using an `if` statement to check if a number is even or odd.

12. Explain the difference between `while` and `do-while` loops in Java. Provide a `do-while`
example to print numbers from 1 to 5.

13. Write a Java code snippet using nested `if-else` statements to categorize a person’s age into
child, teenager, or adult.

14. Demonstrate the use of a `for-each` loop in Java to iterate over an array of strings containing
fruit names.

15. Declare and initialize a one-dimensional array of doubles in Java with five elements, then
calculate their sum.
16. What is a nested loop in Java? Write a program to print a 3x3 matrix using nested `for` loops.

17. Explain method overloading in Java. Write two overloaded methods to calculate the area of a
rectangle (with integer and double parameters).

18. Describe pass-by-value in Java. Write a method that attempts to modify a passed integer
parameter and explain the output.

19. Implement a binary search method in Java to find an element in a sorted integer array and
return its index.

20. Write a Java program to perform selection sort on an array of integers in ascending order.

21. How can you initialize a String in Java? Write code to compare two strings using the
`equals()` method.

22. Explain the difference between the `String` and `StringBuffer` classes in Java. Demonstrate
appending text using `StringBuffer`.

23. Write a Java program to reverse a string using the `StringBuffer` class’s `reverse()` method.

24. What are instance variables in Java? Provide an example of an instance variable in a "Person"
class with a default value.

25. Differentiate between local, instance, and class (static) variables in Java. Provide a code
example showing all three.

26. What is a constructor in Java? Write a default constructor and a parameterized constructor for
a "Phone" class.

27. Explain the purpose of a static method in Java. Write a static method to calculate the factorial
of a number.

28. What does the `final` keyword mean when applied to a method in Java? Provide an example
of a final method in a superclass.

29. Describe the visibility labels `public`, `private`, and `protected` in Java. Which one restricts
access to only the class?

30. Create an abstract class "Vehicle" with an abstract method "startEngine()" and implement it
in a "Car" subclass with specific behavior.

You might also like