Sunteți pe pagina 1din 15

Q-1Give the output of following code:

class inherit_single
{ protected int a;
inherit_single()
{ System.out.println(“inside Superclass Constructor”);
a = 10;}
}
class subclass extends inherit_single
{ subclass()
{ System.out.println(“inside subclass Constructor”);
a=11; }
void display()
{ System.out.println(a); }
}
class mainclass
{ public static void main (String args[])
{ subclass obj = new subclass();
obj.display(); }
}

Answer:
Output
inside super class constructor
inside subclass constructor
11
(this is because as the object is created, constructor is called and the constructors are
called in
the order of derivation. First superclass constructor is called , then subclass constructor
and then
fuction.

Q-2write the output of following code;


a. public class test
{ public static void main(String args[])
{ int x[] = { 1, 2, 3, 4};
int y[] = x;
x = new int[2];
for (int i=0; i<x.length; i++)
System.out.println( y[i] + “ “);
} }
Answer:=1
2

Q-3 What do you mean by platform independence of java?

Answer - Platform Independence means that the same program works on any
platform (operating system) without needing any modification .A java source code can
run on any operating system ,hence java is platform independent.

Q-4What is JVM? Is it platform independent?

Answer- JVM ( Java Virtual Machine ) is a specification that provides run time
environment in which java byte code can be executed. As the name implies , the
JVM acts as a virtual machine or processor. Java's platform independence
consists mostly of its Java Virtual Machine (JVM) . JVM makes this possible
because it is aware of the specific instruction lengths and other
particularities of the platform (Operating System).
The JVM is not platform independent. Java Virtual Machine (JVM) provides
the environment to execute the java file. So at the end it's depends on your
kernel and kernel is differ from OS (Operating System) to OS. The JVM is
used to both translate byte code into the machine language for a particular
computer, and actually execute the corresponding machine language
instructions as well. Without the JVM, you can’t run a Java application.

Q-5What is constructor overloading ? Explain with help of example.

Answer-Constructor overloading is a concept of having more than one constructor


with different parameters list, in such a way so that each constructor performs a
different task.

Example-class Box
{
double width;
double height;
double depth;

Box(double wid, double hei, double dep)


{
System.out.println("Constructing Box");
width = wid;
height = hei;
depth = dep;
}
Box()
{
width=-1
height=-1
depth=-1

double volume()
{
return width * height * depth;
}
}
The above is an example of constructor overloading where there
are two costructors named Box and each has different arguments
and different definitions.

Q- 6Differentiate between the following.

a) Overloading and Overriding


Overloading:= This means when two or more methods in one
class have same method name but different parameters.
Overriding:= This means having two methods with same
method name and parameters. One method is in the parent
class and the other is in the child class. Overriding
allows specific implementation of a method in child class
that is already provided in the parent class.

b)Call by value and Call by reference

Call by value:= This approach copies the value of an


actual argument into the formal parameter of the
subroutine/method.Therefore changes made on parameters
of subroutine have no effect on actual arguments.

Call by reference:=In this approach, a reference to


An argument is passed to the parameter and inside the
Subroutine/method,This is used to access the actual
argument specified in the call which means changes
made to the parameter will effect the argument used
to call subroutine.
c)Early binding and Late binding
Late Binding:= Normally java resolves csll to method
Dyanamically at run time. This is called late
binding.

Early binding:=When a function is declared as final,


that method becomes inline. That means java compiler
Copies the bytecode of that subroutine directly
inlined with the compiler code.So since final method
cannot be overridden so the call to that function can
be resolved at compile time.This is called early
binding

Q- 7 Can we overload main function?

Answer:= Yes we can overload the main method in java . But the program doesn’t
execute

the overloaded main method. Overloaded method has to be called from actual main
method only

Q-8 What is an interface? How is it different from abstract class.?Can we create


instance of interface?Is it possible to declare variable in that ?give eg
Answer:=Using the keyword interface, you can fully abstract a class’ interface from is
implementation.That is using interface you can specify what a class must do but not
how it does it.

In abstract class there is atleast one function that is not abstract but in interface all the
functions are abstract.

Q-9 Write a program to accept 10 integers in an array and print the same using
foreach loop

Answer:=

import java.util.*;

import java.io.*;

class Arr

public static void main(String args[])

Scanner sc =new Scanner(System.in);

int a[]=new int[10];

for(int i=0;i<10;i++)

System.out.println("Enter a number");

int num=sc.nextInt();

a[i]=num;

for(int j:a)

{
System.out.println(j);

Q-10 Write a method in java to swap two number. Make use of it in the main function to
read values of two integer variable a and b.Invoke the method swap and display the
modified values of a and b.

Answer:=

import java.util.*;

import java.io.*;

class AB

int num1;
int num2;

AB(int a,int b)

num1=a;

num2=b;

void swap()

int c;

c=num1;

num1=num2;

num2=c;

System.out.println("value of number1 is "+num1);

System.out.print("value of number2 is"+num2);

System.out.print("values are swapped");

class Main_class

public static void main(String args[])

throws IOException

Scanner sc = new Scanner(System.in);

System.out.println("enter a number");
int a=sc.nextInt();

System.out.println("enter another number");

int b=sc.nextInt();

AB obj=new AB(a,b);

obj.swap();

Q-11 What is the prototype of main function in java?

Answer:= Public:=Public keyword is an access modifier which allows the programmer to


control validity of class members.When a class member is preceeded by public then that member
may be exceeded by the code outside the class in which it is declared.

Static:=Keyword static allows main function to be called without having any instance of that
class.

Void:=It simply tells that main function would not return anything.

String [] Arguments:=It is the array of string objects.This means that main function expects an
array of strings.This array typically holds all command line parameters argument passed in when
the program is run from command line.

Q-12Write a java code to calculate factorial of a no. taken at commandline

Answer:=

import java.util.*;

import java.io.*;

class Factorial

public static void main(String args[])

throws IOException
{

Scanner sc=new Scanner(System.in);

System.out.print("enter a number to compute factorial");

int num=sc.nextInt();

int fact=1;

for(int i=1;i<=num;i++)

fact=fact*i;

System.out.println("factorial of a number is"+fact);

Q-13 What is a java package ?Which package is imported by default?

Answer:= It is a mechanism to encapsulate group of classes, interfaces and sub packages.

The default package available in java is Java.lang

Q-15What is the use of final keyword with respect to variable , method and class?

Answer:=

When a final keyword is used with a method, that method cannot be overridden.

Final keyword is used with class if we want to prevent that class to be inherited .Declaring a
class as final implicitly declares all its methods are final too.

When you declare a variable constant , it will be then readable only , not writable. It will
become constant and its value wont be changed.

Q-16 What is a Static keyword .Explains its usage.

Answer:= To define a class member that will be used independent of any object of that class, use
a keyword “static”.When a member is declared as static , it can be used / accessed before any
object of its class are created.

Both data members and member functions of a class can be declared as static.
Static keyword makes the program memory efficient.It can be used to refer to common property
of all the objects that is not unique in every object. Static variables get memory only once in the
class.

Q-17Write a java program to perform the arithmetic operations using the concept of
method overloading, define a method add that accepts as input to int values and return
their sum as int. Overload this method by a method that accepts two float values and
return a float.
Answer:

class Overload

double add(int a,int b)

return a+b;

double add(float a,float b)

return a+b;

class Add

public static void main(String args[])

Overload obj=new Overload();


int num1=obj.add(3,5);

System.out.print("addition of integers "+num1);

int num2=obj.add(10.3,10.23);

System.out.print("addition of float numbers"+num2);

Q:-18Can we declare a class as static? Explain.

Answer:-You can declare a class inside top level as static in java.Such classes are also known as
nested classes and they can be declared as static.

But making a top level class static in java is not allowed.

Q-19 Can an interface implement or extend another interface?

Answer:=Implements denotes defining implementation of the methods of the interface but


interfaceshave no implementations so that is not possible.
Q- 20What is super keyword? Explain its usage with help of example.

Answer:= Whenever a subclass needs to refer to it.s immediate superclass, it can do so by super
keyword. Super has 2 general forms the first calls the superclass constructor and the second is
used to access member of superclass that has been hidden by members of subclass.

Usage

1)Super.variable_name refers to the variable of the parent class.

2)Super() invokes the constructor of immediate parent class

3)Super.method_name refers to the method of the parent class

Q-22What is “this” keyword?

Answer:=This keyword can be used inside any method to refer to the current object. ”this”
pointer always a reference to the objecton which the method was invoked. This keyword can be
used to resolve name space collision.

Q –23 What is default constructor?

Answer:= A constructor is called defasult constructor when it doesn’t have any parameter.

Q- 24What is garbage collection?

Answer:=Since the objects are dyanamically located using “new” operator .Instead using of
delete operator to deallocate a memory as in C++, java handles deallocation automatically.This
technique is known as garbage collection.

When no reference to an object is made,it is assumed that the object is no longer needed and the
memory occupied by the object can be reclaimed with help of garbage collector. Garbage
collection occurs during the execution of your program..

Q-25Define a class Person having name as a data member. Inherit two more classes
Student and Employee from class Person. To class Student, add data members course,
marks and year, and to class employee add data members department and salary. Write
display method in all three classes to display the relevant details of the corresponding class.
Provide the necessary method to provide dynamic method dispatch.

Answer:

class Person

String name="Raj";

void display()

System.out.println("name of the employee is"+name);

class Student extends Person

String course="MTS";

int year=2018;

int marks=150;

void display()

System.out.println("Course"+course);

System.out.println("year"+year);

System.out.println("Marks"+marks);

class Employee extends Person

{
String Department="Maths";

int Salary=200000;

void display()

System.out.print("Department"+Department);

System.out.print("salary"+Salary);

class College

public static void main(String args[])

Person p=new Person();

Student s=new Student();

Employee e=new Employee();

Person r;

r=p;

p.display();

r=s;

s.display();

r=e;

e.display();

S-ar putea să vă placă și