Professional Documents
Culture Documents
49. State whether the following statements are true or false: (Nov-11)
(i) The elements in an array must be of primitive data types.
(ii) When invoking a constructor from a subclass, its super class’s no-arg constructor is
always invoked.
(iii) A protected data or method can be accessed by any class in the same package.
i) False
ii) True
iii) True
iv) False
50. Explain inner class and working of concatenation operator + by giving examples.
A->
Inner class means one class which is a member of another class. There are
basically four types of inner classes in java.
1) Nested Inner class
2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
class TestStringConcatenation1{
public static void main(String args[]){
String s="Sachin"+" Tendulkar";
System.out.println(s);//Sachin Tendulkar
}
}
Output:Sachin Tendulkar
51. Give output of the following program: (Nov-11)
public class Test {
public static void main(String args[]) {
Count myCount = new Count();
int times=0;
for(int i=0;i<100;i++)
increment(myCount,times);
System.out.println("count is "+myCount.count);
System.out.println("times is "+times);
}
public static void increment(Count c,int times) {
c.count++;
times++;
}
}
class Count {
public int count;
Count(int c){ count=c; }
Count(){ count=1; }
}
A->
the output will be
count is 100
times is 06
52. Design a class named Fan to represent a fan. The class contains:
- Three constants named SLOW, MEDIUM and FAST with values 1, 2 and 3
to denote the fan speed.
- An int data field named speed that specifies the speed of the fan (default
SLOW).
- A boolean data field named f_on that specifies whether the fan is on
(default false).
- A double data field named radius that specifies the radius of the fan
(default 4).
- A data field named color that specifies the color of the fan (default blue).
- A no-arg constructor that creates a default fan.
- A parameterized constructor initializes the fan objects to given values.
- A method named display() will display description for the fan. If the fan is
on, the display() method displays speed, color and radius. If the fan is not on,
the method returns fan color and radius along with the message “fan is off”.
Write a test program that creates two Fan objects. One with default values
and the other with medium speed, radius 6, color brown, and turned on
status true. Display the descriptions for two created Fan objects.
A->
class Fan
{
public static final int SLOW=1,MEDIUM=2,FAST=3;
int speed;
boolean f_on;
double radius;
String color;
Fan()
{
speed=SLOW;
f_on=false;
radius=4;
color="blue";
}
void display()
{
if(f_on==true)
{
System.out.println("Fan is on \n the speed is ="+speed+"\n the color is ="+color+"\n the
radius is ="+radius);
}
else
{
System.out.println("Fan is off \n the color of fan is ="+color+"\n the radius of fan is
="+radius);
}
}
OUTPUT:
A->
class Rectangle
{
double x,y,height,width;
Rectangle()
{
x=0;
y=0;
height=1;
width=1;
}
double getArea()
{
return height*width;
}
double getPerimeter()
{
return 2*(height+width);
}
}
}
OUTPUT:
54. It is required to compute SPI (semester performance index) of n
students of your college for their registered subjects in a semester. Declare
a class called student having following data members: id_no ,
no_of_subjects_registered, subject_code , subject_credits, grade_obtained
and spi.
- Define constructor and calculate_spi methods.
- Define main to instantiate an array for objects of class student to process
data of n students to be given as command line arguments.
A->
import java.util.Scanner;
class Student
{
int id_no,no_of_sub_registered;
int[] sub_code=new int[7];
int[] sub_credit=new int[7];
String[] grade_obtain=new String[10];
int[] grade_point=new int[10];
double spi;
int sum=0,sum1=0;
Student()
{
no_of_sub_registered=7;
sub_code[0]=150001;
sub_credit[0]=2;
sub_code[1]=150701;
sub_credit[1]=5;
sub_code[2]=150702;
sub_credit[2]=6;
sub_code[3]=150703;
sub_credit[3]=6;
sub_code[4]=150704;
sub_credit[4]=4;
sub_code[5]=150705;
sub_credit[5]=1;
sub_code[6]=150606;
sub_credit[6]=6;
}
void calculate_spi()
{
for(int i=0;i<=6;i++)
{
if(grade_obtain[i].equals("AA"))
grade_point[i]=10;
else if(grade_obtain[i].equals("AB"))
grade_point[i]=9;
else if(grade_obtain[i].equals("BB"))
grade_point[i]=8;
else if(grade_obtain[i].equals("BC"))
grade_point[i]=7;
else if(grade_obtain[i].equals("CC"))
grade_point[i]=6;
else if(grade_obtain[i].equals("CD"))
grade_point[i]=5;
else if(grade_obtain[i].equals("DD"))
grade_point[i]=4;
else if(grade_obtain[i].equals("FF"))
grade_point[i]=0;
this.sum = sum+(sub_credit[i]*grade_point[i]);
this.sum1 = sum1+sub_credit[i];
spi = sum/sum1;
System.out.println("your spi is = "+spi);
}
if(length <= 0)
{
System.out.println("enter enrollment no list");
}
for(int i=0;i<=6;i++)
{
System.out.print(obj.sub_code[i]+"\t \t \t"+ obj.sub_credit[i] +"\t \t \t");
String r=jaimin.nextLine();
obj.grade_obtain[i]=r;
obj.calculate_spi();
}
}
}
OUTPUT:
55. Declare a class called coordinate to represent 3 dimensional Cartesian
coordinates( x, y and z). Define following methods:
- constructor
- display, to print values of members
- add_coordinates, to add three such coordinate objects to produce a
resultant coordinate object. Generate and handle exception if x, y and
z coordinates of the result are zero.
- main, to show use of above methods.
A->
import java.lang.Exception;
}
Cartesian(double x,double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
void display()
{
System.out.println("value of x = "+x);
System.out.println("value of y = "+y);
System.out.println("value of z = "+z);
}
OUTPUT:
A->
import java.util.Scanner;
class Employee
{
String[] employee_id;
String[] employee_name;
}
Salary(int j)
{
/*initialization of array */
employee_name=new String[j];
employee_id=new String[j];
Designation=new String[j];
monthly_salary= new double[j];
void display(int j)
{
System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------");
System.out.println("\t Details of employee who have salary above 20000");
System.out.println("---------------------------------------------------------------");
System.out.println("---------------------------------------------------------------\n \n");
if(monthly_salary[i]>=20000)
{
System.out.format("%-15s %-15s %-25s %-10s
%n",employee_id[i],employee_name[i],Designation[i],monthly_salary[i]);
}
}
}
if(length==0)
{
System.out.println("please enter employee id");
}
for(int i=0;i<length;i++)
{
obj.employee_id[i]=args[i];
obj.display(length);
}
}
OUTPUT:
57. Define time class with hour and minute. Also define addition method to
add two time objects.
A->
58. Write a program to create circle class with area function to find area of
circle.
A->
class AreaOfCircle
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
System.out.println("Enter the radius:");
double r= s.nextDouble();
double area=(22*r*r)/7 ;
System.out.println("Area of Circle is: " + area);
}
}
OUTPUT:
Enter the radius:
7
Area of circle : 154
59. Use of Inheritance, Inheriting Data members and Methods, constructor in
inheritance, Multilevel Inheritance – method overriding
A->
Use of inheritance
o For Method Overriding (so runtime polymorphism can be achieved).
The inherited fields can be used directly, just like any other fields.
We can declare new fields in the subclass that are not in the
superclass.
The inherited methods can be used directly as they are.
Use of constructor in inheritance
the constructor of a base class used to instantiate the objects of the base class and
the constructor of the derived class used to instantiate the object of the derived class.
With the help of multilevel inheritance we can extend a class in another class, which
extends another class
A->
In Java, Dynamic method dispatch is a technique in which object refers to
superclass but at runtime, the object is constructed for subclass.
B b = new B();
b.m1();
C c = new C();
c.m1();
A a2;
a2 = b;
a2.m1();
a2 = c;
a2.m1();
B b2 = c;
b2.m1();
}
}
OUTPUT: m1-A
m1-B
m1-C
m1-B
m1-C
m1-C
1) Method overloading is used to increase the Method overriding is used to provide the
readability of the program. specific implementation of the method that is
already provided by its super class.
4) Method overloading is the example Method overriding is the example of run time
of compile time polymorphism. polymorphism.
OUTPUT:
//Here, add with two parameter method runs
10
// While here, add with three parameter method runs
17
iv)Hierarchical inheritance
v)Hybrid inheritance
A->
Static binding : when type of the object is determined at compiled time, it is known
as static binding.
class Dog{
private void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Dog d1=new Dog();
d1.eat();
}
}
Example of dynamic binding
class Animal{
void eat(){System.out.println("animal is eating...");}
}
class Dog extends Animal{
void eat(){System.out.println("dog is eating...");}
public static void main(String args[]){
Animal a=new Dog();
a.eat();
}
}
64. Handle multilevel constructors – super keyword, Stop Inheritance – Final keywords
A->
Whenever you create the instance of subclass, an instance of parent class is created
implicitly which is referred by super reference variable.
The final keyword in java is used to restrict the user. The java final keyword can
be used in many context. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no
value it is called blank final variable or uninitialized final variable. It can be initialized
in the constructor only. The blank final variable can be static also which will be
initialized in the static block only.
65. Explain keywords - super and throws
A->
Super is a keyword of Java which refers to the immediate parent of a class and is used
inside the subclass method definition for calling a method defined in the superclass. A
superclass having methods as private cannot be called. Only the methods which are public
and protected can be called by the keyword super. It is also used by class constructors to
invoke constructors of its parent class.
Use of superclass
A->
i)Super
Example:
class employee {
int wt = 8;
}
ii)final
Final is a keyword in Java that is used to restrict the user and can be used in many
respects. Final can be used with:
Class
Methods
Variables
Example:
import java.util.*;
import java.lang.*;
import java.io.*;
class stud {
final int val;
stud() {
val = 60;
}
void method() {
System.out.println(val);
}
public static void main(String args[]) {
stud S1 = new stud();
S1.method();
}
}
67. Explain the followings: (i) this, super, final.
A->
i) This
ii) super
iii)final
Final is a keyword in Java that is used to restrict the user and can be used in many
respects. Final can be used with:
Class
Methods
Variables
68. What are final class, final function and final variable in java? Explain with example.
A->
Final class
Example:
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
}
}
Final function
If you make any method as final, you cannot override it.
Example:
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Final variable
If you make any variable as final, you cannot change the value of final variable(It
will be constant).
Example:
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}
69.Explain Cosmic superclass and its methods.
A->