What is a variable?
Ans. It is nothing but place
holder of data. It is a memory location. Example int a, b; Here a and b is are
variable of datatype int.
State the difference between token and identifier, variable.
Ans. The smallest individual unit
of the program is called Token. e.g: Identifiers, Keyword, Literals, etc. whereas an identifier is a name given to different parts of a program, it is also known as variables. e.g. variable, functions, classes, etc.
Variable: A memory location that
stores a value is known as a variable. It is like a box in memory where we keep or store values. E.g. int a;
Explain the Instance Variable. Give an example.
Ans. Variables associated with
the instance of a class are called instance variables. E.g.
class ABC
{
int a, b, c; // Instance variable
void sum()
{
a = 20;
b = 30;
c = a + b;
System.out.println(c);
}
}
class ABC
{
int a, b, c; // Instance variable
void sum()
{
a = 20;
b = 30;
c = a + b;
System.out.println(c);
}
}
For example the term Object and Class using an example.
Ans. Object: An instance of a
class called Object. The table is an instance of class Furniture.
Class: Blueprint of an object is
called Class. For example, mango, apple and orange are members of the class fruit.
Name four OOP principles.
Ans. Abstraction, Encapsulation,
Polymorphism, Inheritance
Abstraction: Abstraction refers to the act of representing
essential features without including the background details or explanations.
Polymorphism: polymorphism
refers to a programming language's ability to process objects differently
depending on their data type or class. More specifically, it is the ability to
redefine methods for derived classes. For example, given a base class shape,
polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles, and triangles. No matter
what shape an object is, applying the area method to, it will return the
correct results
Encapsulation: The wrapping up of data and methods into a
single unit is called Encapsulation.
.Inheritance: Inheritance
is the process by which objects of one class acquire the properties of objects
of another class. Different kinds of objects often have a certain amount in common
with each other. Mountain bikes, road bikes, and tandem bikes, for example, all
share the characteristics of motorbike. Yet each also defines additional
features that make them different: tandem bicycles have two seats and two sets
of handlebars; road bikes have drop handlebars; some mountain bikes have an
additional chain ring, giving them a lower gear ratio.
Define Byte code and JVM.
Ans. JVM: A JVM is a machine within a machine. it is like a real Java processor,
enabling Java byte code to be executed as actions or operating system calls on
any processor regardless of the operating system.
Byte code is
the compiled format for Java programs. Once a Java program has been converted
to byte code, it can be transferred across a network and executed by Java
Virtual Machine (JVM). Byte code files generally have a .class extension
What Is a Package?
A package is a namespace that
organizes a set of related classes and interfaces. Conceptually we can think of
packages as being similar to different folders on our computer. We might keep
HTML pages in one folder, images in another, and scripts or applications in yet
another. Because software written in the Java programming language can be
composed of hundreds or thousands of individual classes, it makes sense to keep
things organized by placing related classes and interfaces into packages.
Mention any two attributes required for class declaration.
Ans: Variables and function or
state and behavior.
Define Constructor. When the constructor is called?
Ans: A constructor is the special
member function that is automatically called when an instance of a class is
declared.
A constructor is called when an
object of the class is created.
Why do we need a constructor as a class member?
Ans: Constructors have one
purpose in life: to create an instance of a class.
What is a default constructor? Why is a default constructor called so?
Ans: A default constructor is a
special member function that initializes the objects.
It is called because it declares
no parameters. It gets invoked without any parameter
What are the characteristics of constructor function?
Ans: Constructors are the special
member functions and have the same name as class and it is called automatically
when object being create.
What
is parameterized constructor?
Ans: The constructors which can
take different parameters are referred to as Parametrized Constructors.
What is constructor overloading?.
Ans: Defining more than one
constructor for a class where they differ in the parameter types or number of
parameters passed to the constructor is called overloading.
What is a method or function?
Ans. When we
define a set of code within a block having a name is called method. It can call
or invoke at any time.
Example:
int sum(int x, int y)
{
int z=x+y;
return z;
}
What is the Actual parameter?
Ans: The parameter given to the
calling function is called actual parameter. Actual parameters (also
known as arguments) are what are passed by the caller.
What is a Formal parameter?
Ans: the Identifier used in a
method to stand for the value that is passed into the method by a caller.
Formal parameters are also known as parameters of the function.
What is the purpose of the new operator?
Ans: The new operator is used for the allocation of memory. It can be used for the creation of a new object. E.g. Classex ob=new Classex();
Explain the function of a return statement.
Ans: A return statement ends
the processing of the current function and returns control to the caller of the
function.
Q. What is call by
value?
Ans: In call by value arguments are passed by the
value, which means that a copy of the arguments is passed to the method can
make changes to the value of this copy but can not change the values of the
original variables in the calling method.
State the difference
between Constructor and Method.
Constructor
|
Method
|
1.Constructor name is the same as the class name
| The class name should not be the same |
2.
It is used only for initializing data members.
|
Can
do any type of calculation or printing.
|
3.
It has no return type not even void
|
It
has a return type.
|
4.
It is called automatically when object is being created.
|
It
has to be called or invoked.
|
What does the
following mean?
Employee staff = new Employee ( );
Ans: The statement means that staff is an the object of class Employee and it is being created.
State the difference between = = operator and equals ( ) method.
Ans: equals() is string function whereas == is a relational operator. The difference between them is that the equal() method compares the characters inside two string whereas == operator compares the two object references that whether the two objects belongs to the same class or not.
Java: The Complete Reference
What is the role of the keyword void in declaring functions?
Ans: when we want a function that has
no return value is used void.
If a function
contains several return statements, how many of them will be executed?
Ans: One
Which OOP principle
implements function overloading?
Ans: Polymorphism
OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)
Define an impure function and Pure function.
Ans: Pure Function: only returns a value without bringing about
any change in the values being passed to it, is called a pure function. In
other words, there is no change in the state of the function.
Example:
Static
Boolean max(int m, int n)
{
if(m == n)
return true;
else
return false;
}
Impure Function: A function that
brings about a change in the argument that it receives. Its arguments will
always be reference types. It may or may not return value. In other words, an
impure function brings about a change in the state of the function. This change
in state is called the side effect of calling an impure function.
Example:
static
void count(Number n)
{
n.counter=n.counter+1;
}
The
above function takes the argument n as an object of class Number. The counter
variable of object n is incremented by 1 every time this function is called.
Explain function
overloading with an example.
Ans: Function
Overloading.
When more than one function has the same name but the type and number of arguments
are different, they are said to be overload. Thus, the parameter list of each
function should be unique. Function overloading implements the concept of
polymorphism.
Example
of function overloading:
Float
sum(float a, float b)
{
return a+b;
}
int
sum(int a, int b, int c)
{
return a+b+c;
}
What is meant by an
infinite loop? Give an example.
An infinite loop is a loop whose test condition is
always true. This type of loop never ends by itself. For example:
for(i=1;i>0;i++)
{
System.out.println(“Hi”);
}
Explain the function
of each of the following with an example:
continue:
Ans: The continue statement causes the loop to be continued with the next iteration after skipping any statements in between.
Do
{
......................
......................
if(condition)
continue;
}while(test condition);
break;
Ans: An early exit from a loop can be accomplished by using the break statement. When the break statement is encountered inside a loop, the the loop immediately exists.
Ex. for(.................................)
{
..........................
if(condition)
break;
}
State one similarity
and one difference between while and do-while loop.
Ans: While and do while both are to repeat the process as long as the user wants.
The difference between while and do-while is that
while check condition first whereas do while checks the condition at the
end.
State any two objectives of using Arrays.
Ans: 1. Use hold elements in a contiguous memory location.
2. Arrays are used to group storage locations.
Explain the term for
loop with an example.
Ans: The process of repeatedly executing a block of statements is known as
loop.
What are Nested
Loops? Give one example.
Ans: Loop
within a loop is called nested loop.
for(i=0;i<5;i++)
{
for(j=0;j<I;j++)
{
……………….
………………
}
}
Java
Become a Java programmer in 3 hours
Few more Question Answer Topic