Some of the separators that are used in Java. The variables in Java are allowed to get initialized at run time. Such. type of initialization is called dynamic initialization.
Two Marks Questions with Answers
Q.1 Mention some of the separators used in Java
programming.
Ans. :
Some of the separators that are used in Java are as given below –

Q.2 How dynamic initialization of variables is
achieved in java ?
Ans. The
variables in Java are allowed to get initialized at run time. Such. type of
initialization is called dynamic initialization.
For
example
double
a=5.0;
double
b=20.0
double
c=Math.sqrt((a+b));
Here the
expression is evaluated at run time and its square root value is calculated and
then the variable c is initialized dynamically.
Q.3 What is the output of the main method in
the given code ?
public
static void main(String[] args)
{
screen.write(sum());
}
static
int sum()
{
int
A=12;
int
B=13;
return
=A+B;
}
Ans. It
will generate error at the statement return = A+B as "Illegal start of expression".
Q.4 What are the features of Java ?
1) Java
is simple to implement.
2) Java
is platform independent.
3) It is
an object oriented programming language.
4) It is
a robust programming language.
5) Java
is designed for distributed system.
Q.5 Define objects and classes in Java.
Ans. An
object is an instance of a class. The objects represent the real world entity.
The objects are used to provide a practical basis for the real world. Each
class is a collection of data and the functions that manipulate the data. The
data components of class are called data fields and the function components of
the class are called member functions or methods.
Q.6 Why are classes important in OO technology?
Ans.
Classes bind together the relative data and methods in a cohesive unit. Due to
this arrangement, only certain methods are allowed to access the corresponding
data. Secondly, if any modification is needed, then the class can be viewed as
one module and the changes made in one class does not spoil rest of the code.
Moreover, finding error from such a source code becomes simple.
Hence
use of class is very important thing in OO technology.
Q.7 What is the difference between object and
class?
Following
are some differences between the class and the object -

Q.8 What is the difference between structure
and class?

Q.9 What is the difference between static and
non static variables?
Ans. A
static variable is shared among all instances of class, whereas a non static
variable (also called as instance variable) is specific to a single instance of
that class.
Q.10 Define encapsulation.
Ans. Encapsulation
means binding of data and method together in a single entity called class.
Q.11 What is an abstract class?
Ans.
When apply inheritance, the class becomes more and more specific as we move
down. Sometimes a situation may occur that the superclass becomes more general
and less specific. Such a class lists out only common features of other
classes. Such a super class is called as abstract class.
Q.12 Define class. Give example.
Ans. Each
class is a collection of data and the functions that manipulate the data. The
data components of class are called data fields and the function components of
the class are called member functions or methods.
For
example
class
Customer
{
int ID;
String
Name; //Data Field
Customer()
//Constructor
{ }
double
withdraw_money() //method {...}
}
Q.13 What are the benefits of encapsulation?
Should abstractions be user centric or developer- centric ?
Ans. Due
to encapsulation the corresponding data and the methods get bound together by
means of class. The data inside the class is accessible by the function in the
same class. It is normally not accessible from outside component. Thus the
unwanted access to the data can be protected.
The
abstraction should be user centric. While developing the system using the O0
principles it is important to focus the user and not the developer.
Q.14 How would you declare an object of type
animal named lion that takes a weight of 500 and length of 45 as parameters?
Ans.:
public
class Animal
{
int
weight, length;
Animal(int
wt, int len)
{
weight=wt;
length=len;
}
void
Show()
{
System.out.println("\n
The weight of animal is: "+weight);
System.out.println("\n
The length of animal is: "+length);
}
}
class
AnimalMain
{
public
static void main(String args[]).
{
Animal
Lion=new Animal(500,45);
Lion.Show();
}
}
Output
The
weight of animal is: 500
The
length of animal is: 45
Q.15 What is meant by private access specifier?
Ans. The
private access specifier allows classes, methods and data fields accessible
only from within the own class. The functions outside the class cannot access
the data members or the member functions.
Q.16 What do you mean by instance variable?
Ans. :
An Object is an instance of a class. The variables that the object contains are
called instance variables. For example consider a class Student
class
Student
{
int
RollNo;
char
name[10];
}
Student
S; //Creation of object of type Student class.
S= new
Student();
If we
create an object of the class Student say S, then using this object the
variables RollNo and name can be accessed. These variables that belong to
object S are then called as instance variables. Thus the Instance variables are
any variables, without "static" field modifier, that are defined
within the class body.
Q.17 Define constructor.
Ans.
Constructor is a specialized method used for initializing the objects. The name
of this method and the name of the class must be the same. The constructor is
invoked whenever an object of its associated class is created.
Q.18 What is Static in Java ?
Ans. In
java, static is a member of a class that is not associated with an instance of
a class. If there is a need for a variable to be common to all the objects of a
single java class, then the static keyword should be used in the variable
declaration.
Q.19 What is the difference between a
constructor and a method?
Ans. :

Q.20 What are key characteristics of objects?
Ans. :
1.Object
is an instance of a class.
2.Objects
are runtime entities.
3. Using
object of a class the member variable and member functions of a class can be accesses.
Q.21 What is meant by parameter passing
constructors? Give example.
Ans. The
constructor methods to which the parameter are passed are called parameter
passing constructors
Example
-
Rectangle(int
h,int w)
{
height=h;
weight=w;
}
Q.22 Enumerate two situations in which static
methods are used.
Ans. :
1) The
situation in which object of belonging class is not created and we want to use
the method of that class, then the method must be static.
2) For
calling another static method, one such static method is used.
3) For accessing static data the static method
must be used.
Q.23
List any four Java Doc comments.
Ans. :

Q.24 What is the need for javadoc multiline
comments?
Ans.
Javadoc multiline comments can be used to document all java source code.
Comments follow a standard format consisting of a description followed by block
tags. The first sentence of the description should be clear and concise as it
is used in the summary of the API item.
Q.25 Define access specifier.
Ans.
Access Specifier is used to set the accessibility of class members. There are
three access specifiers in Java - (1) Public (2) Private (3) Protected.
Q.26 What is Javadoc ?
Ans. The
Javadoc is a standard way to comment the java code. It has a special format of
commenting the java code.
Q.27 Can Java source file be saved using a name
other than the class name. Justify.
Ans. Yes, we can save the java source file
using a name other than the class name. But we should compile the program with
file name and should run the program with the class name in which the main method
exist. And there must not be any public class defined inside this java source
file. For example, consider following Java code which is saved using the file
name test.java
class A
{
public
static void main(String args[])
{
System.out.println("Class
A");
}
}
class B
{
public
static void main(String args[])
{
System.out.println("Class
B");
}
}
Output
Step 1:
Compile the above program using following command
javac
test.java
Step 2:
Now execute the above program using
java A
The
output will be
Class A
Step 3:
If you execute the above code as
java B
The
output will be
ClassB
Object Oriented Programming: Unit I: Introduction to OOP and Java : Tag: : Introduction to OOP and Java | Object Oriented Programming - Two marks Questions with Answers
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation