Professional Documents
Culture Documents
Oopj Gtu Summer
Oopj Gtu Summer
Oopj Gtu Summer
**Answer:**
|-------------------------------|-------------------------------------------------|--------------------------------------
---|
### Q.1 (b) Explain the Super keyword in inheritance with a suitable example. (4 marks)
**Answer:**
The `super` keyword in Java is used to refer to the immediate parent class object. It can be
used to access parent class variables, methods, and constructors.
*Example:*
```java
class Animal {
void eat() {
System.out.println("Animal is eating");
void eat() {
System.out.println("Dog is eating");
void display() {
```
### Q.1 (c) Define: Method Overriding. List out Rules for method overriding. Write a Java
program that implements method overriding. (7 marks)
**Answer:**
**Definition:**
Method Overriding occurs when a subclass provides a specific implementation for a method
that is already defined in its superclass.
1. The method in the child class must have the same name, return type, and parameter list as
the method in the parent class.
2. The overriding method cannot have a more restrictive access modifier than the overridden
method (e.g., if the parent method is public, the child method cannot be protected).
3. The overriding method can throw narrower or fewer checked exceptions than the overridden
method.
*Example:*
```java
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
```
### Q.2 (a) Explain the Java Program Structure with example. (3 marks)
**Answer:**
3. **Class Definition**
4. **Main Method**
*Example:*
```java
package mypackage;
import java.util.Scanner;
// Class definition
// Main method
System.out.println("Hello, World!");
```
### Q.2 (b) Explain the static keyword with a suitable example. (4 marks)
**Answer:**
The `static` keyword in Java is used for memory management primarily. It can be applied to
variables, methods, blocks, and nested classes.
*Example:*
```java
class StaticExample {
// Static variable
// Static method
// Constructor
StaticExample() {
count++;
```
### Q.2 (c) Define: Constructor. List out types of it. Explain Parameterized and copy constructor
with a suitable example. (7 marks)
**Answer:**
**Definition:**
A constructor in Java is a block of code that initializes a newly created object. It has the same
name as the class and no return type.
**Types of Constructors:**
1. **Default Constructor**
2. **Parameterized Constructor**
3. **Copy Constructor**
```java
class Student {
int id;
String name;
// Parameterized constructor
Student(int i, String n) {
id = i;
name = n;
void display() {
```
```java
class Student {
int id;
String name;
// Parameterized constructor
Student(int i, String n) {
id = i;
name = n;
// Copy constructor
Student(Student s) {
id = s.id;
name = s.name;
void display() {
```
### Q.3 (a) Explain Type Conversion and Casting in Java. (3 marks)
**Answer:**
**Type Conversion:** It is the process of converting one data type into another. Java supports
two types of type conversion:
```java
int i = 100;
long l = i;
float f = l;
double d = 100.04;
long l2 = (long) d;
```
### Q.3 (b) Explain different visibility controls used in Java. (4 marks)
**Answer:**
*Example:*
```java
class Visibility {
obj.display();
```
### Q.3 (c) Define: Thread. List different methods used to create Thread. Explain Thread life
cycle in detail. (7 marks)
**Answer:**
**Definition:**
A thread in Java is a lightweight process that enables multitasking within a program. Threads
share the process's resources but execute independently.
2. **Runnable:** The thread is ready to run and waiting for CPU time.
4. **Blocked/Waiting:** The thread is not eligible to run, waiting for a resource or another thread
to perform a specific action.
10 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
```java
System.out.println("Thread is running");
1.start();
```
```java
System.out.println("Thread is running");
t1.start();
11 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
```
```java
System.out.println("Thread started");
try {
} catch (InterruptedException e) {
System.out.println(e);
System.out.println("Thread ended");
```
### Q.4 (a) Differentiate between String class and StringBuffer class. (3 marks)
**Answer:**
|------------------------|-------------------------------------|-------------------------------------|
12 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
### Q.4 (b) Write a Java Program to find the sum and average of 10 numbers of an array. (4
marks)
**Answer:**
```java
int[] numbers = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int sum = 0;
sum += number;
```
**Abstract Class:**
13 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
An abstract class in Java is a class that cannot be instantiated on its own and is meant to be
subclassed. It can contain both abstract methods (without a body) and concrete methods (with
a body). Abstract methods must be implemented by the subclasses.
2. Can have abstract methods, which are methods declared without an implementation.
**Example:**
```java
// Abstract class
// Regular method
void sleep() {
void sound() {
System.out.println("Dog barks");
14 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
// Animal animal = new Animal(); // This will give an error because Animal is abstract
```
In this example, `Animal` is an abstract class with an abstract method `sound()` and a
concrete method `sleep()`. The `Dog` class extends `Animal` and provides an
implementation for the `sound()` method. The `main` method creates an instance of `Dog`
and calls its methods.
**Final Class:**
A final class in Java is a class that cannot be subclassed. This means no other class can extend
a final class. This is useful when you want to prevent inheritance for security reasons or to
ensure the class's implementation remains unchanged.
**Example:**
```java
// Final class
15 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
void display() {
System.out.println("This is a vehicle.");
// // Compilation Error: The type Car cannot subclass the final class Vehicle
// }
```
### Q.5 (a) Write a program in Java to create a file and perform write operation on this file. (3
marks)
**Answer:**
```java
import java.io.FileWriter;
import java.io.IOException;
16 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
```
### Q.5 (b) Explain throw and finally in Exception Handling with an example. (4 marks)
**Answer:**
**throw:** The `throw` keyword is used to explicitly throw an exception from a method or any
block of code.
**finally:** The `finally` block is used to execute important code such as closing resources,
and it will always execute regardless of whether an exception is thrown or not.
*Example:*
```java
} else {
17 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
try {
checkAge(15);
} catch (ArithmeticException e) {
} finally {
```
### Q.5 (c) Describe Polymorphism. Explain run-time polymorphism with a suitable example in
Java. (7 marks)
**Answer:**
**Polymorphism:**
Polymorphism in Java allows methods to perform different tasks based on the object that it is
acting upon. It can be achieved by method overloading (compile-time polymorphism) and
method overriding (run-time polymorphism).
**Run-time Polymorphism:**
*Example:*
```java
18 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
class Animal {
void sound() {
@Override
void sound() {
System.out.println("Dog barks");
@Override
void sound() {
System.out.println("Cat meows");
19 | P a g e DHYAN PATEL
OOPJ GTU SUMMER 2023
```
20 | P a g e DHYAN PATEL