CHAPTER-5
JAVA GUI PROGRAMMING REVISION TOUR - III [Methods etc.]
Brief Summary of the Chapter:
In this chapter concept
related with Class, Objects, Constructors and methods are discussed. In Java method or function is a sequence of some declaration and
executable statements.
In Java, which is strictly Object-oriented, any action can take place through methods and methods have to be exist as a part of the class.
In Java, which is strictly Object-oriented, any action can take place through methods and methods have to be exist as a part of the class.
Key points:
· Methods is a sequence of statements that
carry out specific tasks. · Methods returns a value through return
statement.
· Class is a blue print for creating objects of a certain charactersticks. · Class contains fields and methods.
· Class is a blue print for creating objects of a certain charactersticks. · Class contains fields and methods.
· Classes created through keyword class.
· Object is instance of a class created through
new operator.
· Constructor is method with the same name as
of that class it is used to initialized object of
class.
class.
· Constructor can either be parameterized or
non-parameterized. · The
“this” keyword is used to refer to current object.
SOLVED QUESTIONS
1. In java, methods
reside in __________.
(a) Function (b) Library
(c) Classes (d) Object Ans: (c) Classes
2. The number and type of
arguments of a method are known as _____________.
(a) Parameter list (b)
Calling (c) Definition (d)None to these.
Ans: (a) Parameter list
3. The first line of method definition that
tells about the type of return value along with number and type of arguments is
called_____________.
(a) Class (b) Object (c)
Prototype (d) Datatype Ans: (c) Prototype
4. A member method having
the same name as that of its class is called______method.
(a) Destructor (b)
Constructor (c) Object (d) Variable
Ans: (b) Constructor
5. A constructor method
has__________return type.
(a) float (b) void (c) no
(d) int
Ans: (c) no
6. A_________constructor takes no arguments.
(a) Copy constructor (b)
Non-Parameterized constructor (c) Parameterized constructor Ans: (b)
Non-Parameterized constructor
7. A_________constructor
creates objects through values passed to it.
(a) Copy constructor (b)
Default constructor (c) Parameterized constructor Ans: (c) Parameterized
constructor
8. The
keyword_________refers to current object.
(a) void (b) goto (c)
this (d) null
Ans: (c) this
9. Define a method. What
is method prototype and signature?
Ans:A message to an object is a call to the
object’s method requesting that it performs some specified action.
int absval(int a) {
return(a<0?-a:a);
}
The first line of the method definition is
the prototype of the method i.e. the prototypes of method defined above is:
int absval(int a)
10. How are following passed in Java: (i)
primitive types (ii) reference types? Ans: (i) By Value (ii) By reference
11. The String objects being reference types
are passed by reference but changes, if any, are not reflected back to them.
Why?
Ans: The String objects are immutable in
Java, which means once they are
created, the cannot
changed. That is why, even though Strings are passed by reference, they cannot be changed.
changed. That is why, even though Strings are passed by reference, they cannot be changed.
12.At what time is the
constructor method automatically invoked?
Ans: Every time an object
is created, the constructor method is automatically invoked.
13. What are Composite
and user defined data types?
Ans: The data types that
are based on fundamental or primitive data types, are known as Composite
Datatypes. Since these data types are created by users, these are also known as
User Defined
Datatypes.
Datatypes.
14. Can you refer to a class as a composite
type/ user-defined type?
Ans: Yes, class is referred to as a composite type/user defined type.
Ans: Yes, class is referred to as a composite type/user defined type.
15.How is a constructor
invoked?
Ans: A constructor is
automatically called with a new operator in order to create a new object.
16. Which method of a class is invoked just
once for an object? When? Ans: The constructor method.
It is invoked for
initializing values of the object at the time of its creation.
17. Passing the address means call by value
or call by reference? Ans: Call by reference.
18. What’s wrong with the
following constructor definition for the class PlayInfo?
public void PlayInfo( int sticks)
public void PlayInfo( int sticks)
{
nsticks
= sticks;
}
Ans: A constructor cannot
have a return type, not even void.
19. How many values can
be returned from a method?
Ans: Only one value can be returned from
a method though a method can have
multiple return statements but only one gets executed which is reached first
and thus returns the value.
20. What do you
understand by Class and Object?
Ans: The basic unit of OOP
is the Class. It can be described as a blue print of Objects. In other words,
an Object is an instance of a class. A JAVA program may have various class definitions.
An Object is an entity having a unique Identity, characteristics (Properties) and Behavior (Methods).
an Object is an instance of a class. A JAVA program may have various class definitions.
An Object is an entity having a unique Identity, characteristics (Properties) and Behavior (Methods).
22. What is the
difference between instance and static variable?
Ans:
Instance Variable- These
data member are created for every object of the class i.e. replicated with objects.
Class
variable (static)- These
data members that is declared once for each class and all objects share these
members. Only a single copy is maintained in
the memory. These are
declared with static keyword.
23. What do you
understand by constructor in OOP?
Ans: A Constructor is a member method of a
class, used to initialize an Object, when it is created (instantiated).
24.What are the
properties of Constructor?
Ans: There are some properties of constructor:
· A Constructor must have the same name as the
class name and provides initial values to its
data members.
data members.
· A constructor have no return type not even
void.
· JAVA automatically creates a constructor
method, if it is not defined with default values.
25.What
do you understand by methods? What are the advantages of methods?
Ans: Definition: A Method or function is sequence of
statement which are written to perform a
specific job in the application. In Object
Oriented Programming, Method represents the behavior of the object. A message can
be thought as a call to an object’s method.
The following three
advantages/reasons describes that why we use methods.
To cope with complexity:
When programs become more
complex and big in size, it is best technique to fol
conquer” i.e. a complex problem is broken in
to smaller and easier task, so that we can make it manageable. Some times it
is also called Modularization.
Hiding Details:
Once a method is defined, it works like a
Black-box and can be used when required, without concerning that “How it Works?”
Reusability of code:
Once a method is implemented, it can be
invoked or called from anywhere in the program when needed i.e. Method can be
reused. Even a packaged method may be used in
multiple applications. This saves our time
and effort. Most of the method like Math.sqrt() is available as ready to use which can
be used anywhere in the application.
26. How to define a
method?
Ans: A method must be defined before its use.
The method always exist in a class. A Java Program must contain a main() method
from where program execution starts. The general form of defining method is as-
[Access
specifier]<return_type> <method_name>(<parameter(s)>) {…………….
;
body of the method i.e. statement (s);
}
}
Access Specifier:
It specified the access
type and may be public or protected or private.
Return Type:
Specifies the return data
type like int, float etc. Void is used when nothing is to be returned.
Method Name:
Specified the name of
method and must be a valid Java identifier.
Parameters List:
It is list of variable(s), also called Formal
Parameter or Argument, which are used to catch the values when method is invoked.
Also a method may have no parameters.
27.What are the way to
pass values to methods in Java?
Ans: You can pass
arguments (Actual parameters) to method (Formal Parameters) using valid data
types like int, float, byte, char, double, boolean etc. or Reference data type like Object and
Arrays.
types like int, float, byte, char, double, boolean etc. or Reference data type like Object and
Arrays.
A method can called in two
ways -
Call by Value: In this method, the values of Actual
parameters are copied to Formal parameters, so
any changes made with Formal parameters in Method’s body, will not reflected back in the calling
function.
any changes made with Formal parameters in Method’s body, will not reflected back in the calling
function.
The original value of
Actual parameters is unchanged because the changes are made on copied value.
Call by Reference:
In Reference method, the
changes made on the formal parameters are reflected back in the Actual
parameters of calling function because
instead of values, a Reference (Address of Memory location) is passed.
In general, all primitive data types are
passed by Value and all
Reference types (Object, Array) are passed by Reference..
Reference types (Object, Array) are passed by Reference..
28. Differentiate between
constructor and method.
Ans: Though Constructor are member method of
the class like other methods, but they are different from other method
members-
Constructor creates
(initializes) an Object where a method is a group of statements which are packaged to perform a
specific job.
Constructor has no return
type, even void also. Whereas method may have any return type including
void.
void.
The Constructor has the same name as Class,
but method may have any name except Class name.
It is called at the time of object creation, but a method can be called any time when required.
It is called at the time of object creation, but a method can be called any time when required.
29. What is “this”
keyword?
Ans: As you are aware that
static data and method members of a class is kept in the memory in a
single copy only. All the object are created
by their instance variables but shares the class variables (static) and member
methods.
No comments:
Post a Comment