Class Test 1 Solution
Class Test 1 Solution
Class Test-1
Note: Attempt any four questions. Each question is carrying equal marks.
What are Packages in java? How a user defined package is created in CO2 K2
1 java, explain with example? 5
What are Exceptions and how they are handled in java? Explain try CO2 K2
2 5
catch block with example?
Write a program that executes two threads. One thread will print even CO2 K3
3 5
numbers and another thread will print odd numbers from 1 to 5
Write a program in Java to demonstrate use of this keyword in CO2 K2
4 5
constructor.
Why multiple inheritance is not possible in java? How to implement CO2 K2
5 5
multiple inheritance in java? Write code for both conditions.
Solution:
Q1. What are Packages in java? How a user defined package is created in java, explain with
example?
Ans1: A java package is a group of similar types of classes, interfaces and sub-packages.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
User Defined package:
package mypack;
public class A{
public void display(){
System.out.println(“this is my new mypackage”);
}
}
Save this file as A.java and compile by using the command: javac –d . A.java
This will create a .class file in mypack folder.
Program to use the package:
import mypack.*;
public class B {
public void show(){
System.out.println(“this is class B”);
}
public static void main(String args[]){
B obj=new B();
obj.show();
obj.display();
}
}
Q2. What are Exceptions and how they are handled in java? Explain try catch block with example?
Ans 2: An exception in Java is an event, which occurs during the execution of a program,that disrupts
the normal flow of the program's instructions. When an error occurs within a method, the method
creates an object and hands it off to the runtime system.
Types of Exception:
1. Checked Exception (compile time exception)
2. Unchecked Exception (run time exception)
3. Error (unavoidable system error)
public class A {
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
System.out.println("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Q3. Write a program that executes two threads. One thread will print even numbers and another
thread will print odd numbers from 1 to 5.
Ans 3: class A extends Thread {
public void run() {
for(int i=1;i<=5;i++){
if(i%2==0)
System.out.println(“Even value”+i);
} }}
class B extends Thread {
public void run() {
for(int i=1;i<=5;i++){
if(i%2!=0)
System.out.println(“Odd value”+i);
} }}
class C {
public static void main(String args[]) {
new A().start();
new B().start();
}
}
Q4. Write a program in Java to demonstrate use of this keyword in constructor.
Ans:
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Q5. Why multiple inheritance is not possible in java? How to implement multiple inheritance in java?
Write code for both conditions.
Ans: Java does not support multiple inheritance in classes because it can lead to ambiguity. Consider
the following scenario:
class A {
int a=20;
public void display() {
System.out.println("A");
} }
class B {
int a=30;
public void display() {
System.out.println("B");
} }
class C extends A, B //not allowed in java, will give compile time error
{
public void bar() {
System.out.println(“value of a is”+a);
display();
} }
In this example, the class C inherits from both the A and B classes. This means that the C class has
two display() methods and two a variable. When we call the display() method and a variable on a C
object, the compiler will not know which display() method and a variable to call. This is known as the
diamond problem.
How to implement multiple inheritance in Java:
Use interfaces: Interfaces are similar to classes, but they cannot have any concrete methods.
Instead, they only have abstract methods. A class can implement multiple interfaces.
Here is an example of how to use interfaces to implement multiple inheritance:
interface A {
int a=20;
public void display();
}
interface B {
int a=30;
public void display();
}
class C implements A, B {
public void display() {
System.out.println("C");
System.out.println(“value of a in interface A”+A.a);
System.out.println(“value of a in interface B”+B.a);
}
public static void main(String args[]) {
C obj=new C();
obj.display();
}
In this example, the C class implements both the A and B interfaces. This means that the C class has
two display() methods. However, the compiler will not be confused about which display() method to
call because the display() methods in the A and B interfaces are abstract. And all the variables
defined in interface are final static in nature, so we can differentiate variables by using interface
name.
Q6. Compare method overriding and method overloading with suitable example.
Ans:
There are many differences between method overloading and method overriding in java. A list of
differences between method overloading and method overriding are given below:
4) Method overloading is the example Method overriding is the example of run time
of compile time polymorphism. polymorphism.
5) In java, method overloading can't be Return type must be same or covariant in method
performed by changing return type overriding.
of the method only. Return type can
be same or different in method
overloading. But you must have to
change the parameter.
6) 1. Example: 1. Example:
class OverloadingExample{ 2. class Animal{
2. static int add(int a,int b){ 3. void eat(){System.out.println("eating...");
3. return a+b; } 4. } }
4. static int add(int a,int b,int c){ 5. class Dog extends Animal{
5. return a+b+c; 6. void eat(){System.out.println("eating bread...");
6. } } 7. } }