solution of java question paper 2
solution of java question paper 2
1. Define class: A class is a blueprint for creating objects in Java. It defines properties
(fields) and behaviors (methods).
2. One procedural language: C
3. Two keywords of Java: public, static
4. A collection of similar items is called: Array
5. Symbol of increment operator in Java: ++
6. Define keywords: Reserved words in Java that have a predefined meaning (e.g.,
class, if, else).
7. Multiple inheritance can be achieved using: Interfaces
8. JDK stands for: Java Development Kit
9. Byte is a data type to read integer values (True/False): False (Byte stores integer
values, but it’s not used for reading them.)
10. Name of the constructor should be the same as the class name (True/False): True
11. Define encapsulation: Encapsulation is the wrapping of data (variables) and code
(methods) into a single unit, like a class.
12. How to declare a one-dimensional array in Java: int arr[] = new int[5];
13. Four relational operators in Java: ==, !=, >, <
14. Syntax of while loop:
while (condition) {
// code to execute repeatedly
}
15. Define object: An object is an instance of a class that holds data and methods.
16. What is multilevel inheritance: When a class inherits from another class, which in
turn inherits from another class.
17. Define bytecode: Bytecode is the intermediate code generated by the Java compiler,
which runs on the JVM.
18. Define interface: An interface is a reference type in Java that defines abstract
methods that must be implemented by a class.
19. What is an exception: An event that disrupts the normal flow of a program, handled
using try-catch.
20. Can we create an object of a class? Yes, using new ClassName();, except for
abstract classes.
21. What is a subclass? A subclass is a derived class that inherits from a parent
(superclass).
22. Purpose of Scanner class: It is used to take user input in Java (Scanner sc = new
Scanner(System.in);).
Benefits:
Code reusability: You can use existing code without rewriting it.
Easier maintenance: Changes in the superclass can automatically affect derived
classes.
24. Give any two differences between while and do while loop.
While Loop:
Condition Check: The condition is checked before the execution of the loop body. If
the condition is false, the loop body will not execute at all.
Syntax:
while (condition) {
// code block
}
Do While Loop:
Condition Check: The loop body executes at least once, as the condition is checked
after the execution of the loop body.
Syntax:
do {
// code block
} while (condition);
1. Efficient Storage: Arrays store multiple elements of the same data type in a single
variable, which helps in efficient memory management.
2. Fast Access: Elements can be accessed quickly using their index, making searching
and sorting operations much faster compared to other data structures.
Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
// Usage
Animal myDog = new Dog();
myDog.sound(); // Outputs: Dog barks
import java.util.Scanner;
Explanation:
This program prompts the user to enter a number, which it stores in num.
A for loop multiplies all integers from 1 to num to compute the factorial.
An abstract class is a class that cannot be instantiated on its own and may contain abstract
methods (methods without a body). This type of class is used to provide a common interface
for its subclasses.
Purpose: It defines a template for other classes. Subclasses are required to implement
the abstract methods provided in the abstract class.
Example:
// Usage
Shape shape = new Circle();
shape.draw(); // Outputs: Drawing a Circle
1. Definition:
A class defines the blueprint for creating objects and can contain methods and
variables (fields).
An interface is a contract that defines methods that a class must implement, but it
cannot contain any implementation (until Java 8 default methods).
2. Instantiation:
A try block is used in Java to handle exceptions. It allows you to write code that might cause
an exception, and catch that exception to handle it gracefully.
Example:
try {
// Code that may throw an exception
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // This will throw an exception
} catch (ArrayIndexOutOfBoundsException e) {
// Handling exception
System.out.println("Array index is out of bounds!");
}
The Scanner class is part of the java.util package and is used to read input from various
sources (like keyboard input, files, etc.) easily. It simplifies the process of taking user input.
Common Methods:
nextInt(): Reads an integer.
nextLine(): Reads a string.
nextDouble(): Reads a double.
Example:
import java.util.Scanner;
A constructor is a special method that is called when an object of a class is created. It has the
same name as the class and is used to initialize the new object.
Types:
Default Constructor: A constructor with no parameters.
Parameterized Constructor: A constructor that takes parameters to initialize the
object with specific values.
Example:
class Person {
String name;
// Parameterized constructor
Person(String n) {
name = n; // Initialize the name
}
void display() {
System.out.println("Name: " + name);
}
}
// Usage
Person p = new Person("John");
p.display(); // Outputs: Name: John
2. Object-Oriented Languages:
1. If Statement:
if (condition) {
// Code to execute if condition is true
}
2. If-Else Statement:
Purpose: Executes one block of code if the condition is true, and another block if it is
false.
Syntax:
if (condition) {
// Code for true condition
} else {
// Code for false condition
}
3. Else-If Ladder:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the above are true
}
4. Switch Statement:
Purpose: Tests a variable against a list of values (cases) and executes the
corresponding block of code.
Syntax:
switch (variable) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case is matched
}
Q.35 Define Data Type; Explain Numeric and Character Data Types of Java.
(CO2)
Data Type:
A data type defines the type of data a variable can hold. It tells the compiler how to
interpret that data.
1. Integer Types:
Example:
float: 32 bits, used for decimal values, must have an 'f' or 'F' suffix.
double: 64 bits, for larger/double precision decimal values, default for decimals.
Example:
Example:
Q.36 Explain the Concept of Inheritance with Suitable Example, and Explain
Various Types of Inheritances. (CO6)
Inheritance:
Example:
// Main class
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start(); // Inherited method
car.horn(); // Car's own method
}
}
Output:
Vehicle is starting
Car horn is beeping
Types of Inheritance:
1. Single Inheritance:
2. Multiple Inheritance:
A class can inherit features from multiple classes. Java does not support this directly
for classes to prevent ambiguity but allows interfaces.
Example: Class C can implement both Interface I1 and I2.
3. Multilevel Inheritance:
4. Hierarchical Inheritance:
5. Hybrid Inheritance: