Unit-1 Two Marks Questions With Answers
Unit-1 Two Marks Questions With Answers
Ans. : Some of the separators that are used in Java are as given below –
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());
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".
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.
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.
Following are some differences between the class and the object -
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.
Ans. Encapsulation means binding of data and method together in a single entity
called 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.
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;
Customer() //Constructor
{}
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.:
weight=wt;
length=len;
void Show()
class AnimalMain
Lion.Show();
Output
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.
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];
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.
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.
Ans. :
Ans. :
3. Using object of a class the member variable and member functions of a class can
be accesses.
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;
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.
Ans. :
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.
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
System.out.println("Class A");
class B
{
System.out.println("Class B");
Output
javac test.java
java A
Class A
java B
ClassB