Documente Academic
Documente Profesional
Documente Cultură
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.
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.
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.
Example-class Box
{
double width;
double height;
double depth;
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.
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
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
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;
class Main_class
throws IOException
System.out.println("enter a number");
int a=sc.nextInt();
int b=sc.nextInt();
AB obj=new AB(a,b);
obj.swap();
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.
Answer:=
import java.util.*;
import java.io.*;
class Factorial
throws IOException
{
int num=sc.nextInt();
int fact=1;
for(int i=1;i<=num;i++)
fact=fact*i;
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.
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
return a+b;
return a+b;
class Add
int num2=obj.add(10.3,10.23);
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.
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
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.
Answer:= A constructor is called defasult constructor when it doesn’t have any parameter.
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()
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);
{
String Department="Maths";
int Salary=200000;
void display()
System.out.print("Department"+Department);
System.out.print("salary"+Salary);
class College
Person r;
r=p;
p.display();
r=s;
s.display();
r=e;
e.display();