Very Short Question Answers Spring 2023- OOP

Filter Course


Very Short Question Answers Spring 2023- OOP

Published by: Dikshya

Published date: 08 Sep 2023

Very Short Question Answers Spring 2023- OOP

Very Short Question Answers Spring 2023- OOP

1. Why is java called object oriented programming language? 

-Java is called an Object-Oriented Programming (OOP) language because it follows the principles and concepts of OOP. These principles include encapsulation, inheritance, polymorphism, abstraction and everything is treated as an object, and the program structure is based on classes and objects. 

2. What are primitive data types in Java?

- Java has eight primitive data types:

: byte, short, int, long, float, double, char, boolean

3. What are the uses of static and new keywords in java?

-  The ‘static’ keyword is used to declare static variables and methods that belong to a class rather than an instance

- The ‘new’ keyword is used to create an instance (object) of a class. For example, MyClass obj = new MyClass ( ) creates a new instance of the class MyClass.

4. Why is it not possible to use both the abstract and final modifiers simultaneously in Java?

- It's not possible to use both the abstract and final modifiers simultaneously in Java because they have contradictory meanings:

a. abstract: Indicates that a class or method is incomplete and needs to be extended or implemented by subclasses. It allows for further extension and specialization.

b.final: Indicates that a class, method, or variable cannot be extended, overridden, or modified. It denotes the end of the inheritance hierarchy for classes and the immutability of variables.

5. Define multilevel inheritance in Java.

- Multilevel inheritance in Java refers to a situation where a class extends another class, and then another class extends that derived class. It forms a chain of inheritance

6. What are the two ways to implement threads in Java? Include the syntax for each approach.

- The two ways to implement threads in Java are:

a. Extending the thread class:

 class MyThread extends Thread {

    public void run() {

      // Thread logic here

    }

}

// Create and start a thread

MyThread thread = new MyThread();

thread.start();

b. Implementing the runnable interface:

class MyRunnable implements Runnable {

    public void run() {

        // Thread logic here

    }

}

// Create a Runnable and pass it to a Thread

MyRunnable myRunnable = new MyRunnable();

Thread thread = new Thread(myRunnable);

thread.start();

7. Does a single catch statement handle multiple exceptions, explain?

- Yes, a single catch statement can handle multiple exceptions in Java. You can list multiple exception types separated by the vertical bar (|) in the catch block. For example:

try {

    // Code that may throw exceptions

} catch (IOException | SQLException e) {

    // Handle IOException or SQLException

}

8. What is Stream? Why do you need the BufferedInputStream class?

- A Stream in Java is a sequence of data elements that can be processed sequentially. It can represent various data sources, such as files, network connections, or in-memory data structures.

- BufferedInputStream is a class in Java that is used to read data from an input stream more efficiently by buffering the data.

9. Differentiate between Byte Stream and Character Stream.

- Byte Stream: Byte streams in Java, represented by classes like InputStream and OutputStream, are used for reading and writing binary data, such as files.

- Character Stream: Character streams, represented by classes like Reader and Writer , are used for reading and writing character data, typically text

10. Provide definitions and examples of heavyweight and lightweight containers in Java.

- Heavyweight Container: A heavyweight container is a user interface element that is provided by the operating system's windowing system.

- Lightweight Container: A lightweight container is a user interface element that is implemented entirely in Java, without relying on the operating system's windowing system.