0% found this document useful (0 votes)
23 views7 pages

JPR Question Bank - For - PTT1

The document is a question bank for Java programming, covering various topics such as features of Java, constructors, access specifiers, and the differences between classes and interfaces. It includes questions on specific Java concepts like command-line arguments, dynamic method dispatch, and type conversion, along with examples and explanations. Additionally, it addresses inheritance types and provides code snippets for practical understanding.

Uploaded by

shambhupawar293
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)
23 views7 pages

JPR Question Bank - For - PTT1

The document is a question bank for Java programming, covering various topics such as features of Java, constructors, access specifiers, and the differences between classes and interfaces. It includes questions on specific Java concepts like command-line arguments, dynamic method dispatch, and type conversion, along with examples and explanations. Additionally, it addresses inheritance types and provides code snippets for practical understanding.

Uploaded by

shambhupawar293
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
You are on page 1/ 7

JPR Question Bank

Sr.No Questions Mark


. s
1 List any four features of Java 2
1. Object-Oriented
2. Platform Independent
3. Robust
4. Secure
2. Define constructor. List its types 2
A constructor is a special method within a class that is automatically called when an
object 1 of that class is created. Its primary purpose is to initialize 2 the object's data
members (variables) and set up the object for use.
1. Default Constructor
2. Parameterized Constructor:
3. Differentiate between String and StringBuffer class 2

4. Enlist any two access specifier with syntax 2


1. Public: The public access specifier allows access to the class, variable, method,
or interface from any other class or interface, regardless of whether they are in the same
package or a different package. It has the widest scope of all access modifiers.
2. Private:The private access specifier restricts the access of a class, variable,
method, or interface to only within the same class. No other class, even if it's in the same
package, can access a private member. It provides the highest level of encapsulation.

5 Name the wrapper class methods for the following: 2


I)To convert string objects to primitive int.
II)To convert primitive int to string objects
I) To convert string objects to primitive int:

Integer.parseInt(String s): This is the most common way. It parses the string s as a signed
decimal integer.

Integer.valueOf(String s): This returns an Integer object (the wrapper class), which you
can then unbox to an int if needed (often done automatically).

II) To convert primitive int to string objects:

String.valueOf(int i): This is a convenient way to directly convert an int to a String.

Integer.toString(int i): This also converts an int to a String.


6 Describe the use of any methods of vector class with their syntax./Example 2
The Vector class in Java is a dynamic array that can grow or shrink as needed. It's similar
to ArrayList, but Vector is synchronized, making it thread-safe. Here are some commonly
used methods with syntax and examples:

1. add(Object obj): Adds an element to the end of the Vector.

Syntax: boolean add(Object obj)


7 Explain the command line arguments. 2
Command-line arguments are values that you pass to a program when you run it from the
command line (terminal or console). They allow you to customize the behavior of your
program without modifying the code itself. This makes your programs more flexible and
reusable.
8 Describe interface in java with suitable example. 4
An Interface in Java programming language is defined as an abstract type used to specify
the behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface
contains static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction.

By default, variables in an interface are public, static, and final.

It is used to achieve abstraction and multiple inheritances in Java.

It is also used to achieve loose coupling.

In other words, interfaces primarily define methods that other classes must implement.

Syntax

interface {
// declare constant fields
// declare methods that abstract
// by default.
}

interface printable{

void print();

}
class InterfaceExample implements printable{

public void print(){System.out.println("Hello");}

public static void main(String args[]){

InterfaceExample obj = new InterfaceExample();

obj.print();

}
9 Explain dynamic method dispatch in Java with suitable example. 4
Dynamic method dispatch (also known as runtime polymorphism or late binding) is a
mechanism in Java where the correct version of an overridden method is determined at
runtime, not at compile time. This allows for flexibility and extensibility in object-
oriented programming.

How it Works:

Inheritance: Dynamic method dispatch occurs in the context of inheritance, where a


subclass inherits a method from its superclass and overrides it (provides its own specific
implementation).

Polymorphism: A reference variable of the superclass type can hold an object of the
subclass type (this is polymorphism).

Method Call: When you call a method on this polymorphic reference, the Java Virtual
Machine (JVM) determines the actual object's type at runtime and calls the appropriate
overridden method based on that type. This is dynamic method dispatch.

Example:
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}

class Dog extends Animal {


@Override
public void makeSound() {
System.out.println("Woof!");
}
}

class Cat extends Animal {


@Override
public void makeSound() {
System.out.println("Meow!");
}
}

public class Main {


public static void main(String[] args) {
Animal myPet; // Superclass reference

myPet = new Dog(); // myPet now refers to a Dog object


myPet.makeSound(); // Calls Dog's makeSound() - Dynamic dispatch

myPet = new Cat(); // myPet now refers to a Cat object


myPet.makeSound(); // Calls Cat's makeSound() - Dynamic dispatch

Animal anotherAnimal = new Animal();


anotherAnimal.makeSound(); // calls Animal's makeSound()

}
}
10 Define a class circle having data members Pi and radius. Initialize and display values of 4
data members also calculate area of circle and display it.
class Circle {
// Data members
double pi; // Making pi a double for more precision
double radius;

// Constructor to initialize data members


public Circle(double radius) {
this.pi = Math.PI; // Using the built-in Math.PI for accuracy
this.radius = radius;
}

// Method to display the values of data members


public void display() {
System.out.println("Radius of the circle: " + radius);
System.out.println("Value of Pi: " + pi); // You can display Pi if needed.
}

// Method to calculate and display the area of the circle


public void calculateAndDisplayArea() {
double area = pi * radius * radius;
System.out.println("Area of the circle: " + area);
}

public static void main(String[] args) {


// Create a Circle object, initializing it with a radius
Circle myCircle = new Circle(5.0); // Radius = 5

// Display the initialized values


myCircle.display();

// Calculate and display the area


myCircle.calculateAndDisplayArea();

Circle anotherCircle = new Circle(10.0); // Radius = 10


anotherCircle.display();
anotherCircle.calculateAndDisplayArea();

}
}
11 Differentiate between class and interfaces (any four). 4
12 Write implicit and explicit type conversion with example in details. 4
Java provides various data types just like any other dynamic languages such as boolean,
char, int, unsigned int, signed int, float, double, long, etc in total providing 7 types where
every datatype acquires different space while storing in memory. When you assign a value
of one data type to another, the two types might not be compatible with each other. If the
data types are compatible, then Java will perform the conversion automatically known as
Automatic Type Conversion, and if not then they need to be cast or converted explicitly.
For example, assigning an int value to a long variable.
Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically converted. This
happens when:
The two data types are compatible.
When we assign a value of a smaller data type to a bigger data type.
For Example, in java, the numeric data types are compatible with each other but no
automatic conversion is supported from numeric type to char or boolean. Also, char and
boolean are not compatible with each other.

Narrowing or Explicit Conversion


If we want to assign a value of a larger data type to a smaller data type we perform explicit
type casting or narrowing.
This is useful for incompatible data types where automatic conversion cannot be done.
Here, the target type specifies the desired type to convert the specified value to.
13 Explain single and multilevel inheritance with proper example of any one inheritance. 4
1. Single Inheritance

Single inheritance is the simplest form of inheritance. A derived class (also called a child
class or subclass) inherits properties and behaviors from a single base class (also called a
parent class or superclass). Think of it as a "is-a" relationship: a car is a vehicle.

#include <iostream>

#include <string>

// Base class (Vehicle)

class Vehicle {

public:

std::string brand;

std::string model;

Vehicle(std::string b, std::string m) : brand(b), model(m) {}

void displayVehicleInfo() {

std::cout << "Brand: " << brand << std::endl;

std::cout << "Model: " << model << std::endl;

};

// Derived class (Car) inheriting from Vehicle

class Car : public Vehicle {

public:

int numberOfDoors;

Car(std::string b, std::string m, int doors) : Vehicle(b, m), numberOfDoors(doors) {} //


Call base class constructor

void displayCarInfo() {

displayVehicleInfo(); // Accessing base class method

std::cout << "Number of Doors: " << numberOfDoors << std::endl;

}
};

int main() {

Car myCar("Toyota", "Camry", 4);

myCar.displayCarInfo();

return 0;

2. Multilevel Inheritance

Multilevel inheritance involves a hierarchy of classes where a derived class inherits from a
base class, and then another class inherits from that derived class, and so on. It's like a
parent-child-grandchild relationship.

You might also like