0% found this document useful (0 votes)
36 views5 pages

Assignment - 2 - Elementary Concept of Obj - Class

The document discusses the concepts of objects and classes in Java. It defines what a class and object are, and their differences. It describes how classes are used to create objects and act as blueprints. The document also covers class properties like attributes, behaviors, and access specifiers. Multiple choice questions and exercises are provided to test understanding of classes and objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views5 pages

Assignment - 2 - Elementary Concept of Obj - Class

The document discusses the concepts of objects and classes in Java. It defines what a class and object are, and their differences. It describes how classes are used to create objects and act as blueprints. The document also covers class properties like attributes, behaviors, and access specifiers. Multiple choice questions and exercises are provided to test understanding of classes and objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Elementary Concept of

Objects and Classes


A. Tick () the correct option.
1. Which among the following creates a blueprint to represent characteristic and behaviour
of
an object?
a. Object b. Class
c. Instance d. None of these
Ans. b. Class
2. Which among the following do not belong to an object?
a. State b. Behaviour
c. Identity d. Class
Ans. a. State
3. Which among the following is not a component of a class?
a. Modifiers b. Class name
c. Object d. Body
Ans. c. Object
4. Which among the following keyword is used to allocate memory space for an object?
a. new b. for
c. while d. int
Ans. a. new
5. Which among the following operator is used to access individual members of an object?
a. . (dot) b. + (plus)
c. – (minus) d. / (divide)
Ans. a. .(dot)
6. Which among the following modifier is used in a ‘class’?
a. public b. default
c. Both a and b d. None of these
Ans. c. Both a and b
7. Which among the following is a valid class name?
a. Simple Interest b. SimpleInterest
c. 1SimpleInterest d. Simple@Interest
Ans. b. SimpleInterest
8. Which among the following is a valid object name?
a. obj1 b. 1obj
c. Obj 1 d. Obj#1
Ans. a. obj1
9. Which among the following is used to represent behaviour in a class?
a. Method b. Data members
c. Both a and b d. None of these
Ans. a. Method
10. If a method named show( ) is to be invoked using an object ‘ob’ , which among the
following is
correct?
a. ob.show( ) b. ob.show
c. show( ).ob d. None of these
Ans. a. ob.show()
B. State whether the following statements are True (T) or False (F).
1. An object is called a class factory. F
2. A class is an instance of an object. F
3. A class is a mechanism to implement encapsulation. T
4. Data members in a class is used to represent the characteristic of an object. T
5. The new operator is used to create an object. T
6. It’s a rule to have a class-name beginning in capital letter. F
7. Java is case sensitive. T
8. A class-name cannot be a keyword. T
9. The dot operator is used to access members in an object using an object. T
10. In programming every object will have a name. T
C. Fill in the blanks.
1. A class is a template that binds together data and methods together.
2. The values in the attributes of an object is called the state of an object.
3. The . dot operator is used to access the individual members of a class.
4. The keyword new is used to allocate memory space for an object.
5. The default and public access modifier is used with a class.
6. It is a common convention to begin a class-name in capital letter.
7. A class is called an object factory.
8. Object-name is used to give a unique name to an object for identification.
9. Behaviour of an object is represented by methods.
10. The size of fundamental data type is fixed.
SECTION A
Answer the following questions.
1. State two differences between fundamental and user-defined data type.
Ans.
Comparison of Fundamental & User defined data type.
Fundamental data type
1. These are inbuilt data type provided by the Java Language.
2. The size of it is fixed.
3. These data types are available in all parts of a program within a class.
User defined data type
1. These are data types created by the user using fundamental or user defined data
type or both.
2. The size of different user-defined data type depends upon the size of the
individual components of it.
3. These data types are available only as specified by the access specifiers.

2. State two differences between a class and an object.


Ans.
1. A class Class is a blueprint or template from which objects are created.
2. Class is a group of similar objects.
3. Class is a logical entity.

An object
1. Object is an instance of a class.
2. Object is a real world entity such as pen, laptop, mobile, bed, keyboard, mouse,
chair etc.
3. Object is a physical entity.

3. Why is a class called a user-defined data type?


Ans. Primitive data types are the general and fundamental data types that we have in Java
and those
are byte, short, int, long, float, double, char, boolean, etc., User defined data types are
those
that user / programmer himself defines.
4. What is an access specifier? Which two access specifier is used in a class declaration?
Ans. Access modifiers (or access specifiers) are keywords in object-oriented languages
that set the
accessibility of classes, methods, and other members. Access modifiers are a specific part
of
programming language syntax used to facilitate the encapsulation of components.
The public and default access specifier is used in a class declaration.
5. Why is a class called an object factory?
Ans. A class is called an object factory because objects are created from a class. An object is
an
instance of a class.
The following statements create two objects s1 and s2 of the class Student.
Student s1 = new Student();
Student s2 = new Student();
So, we have a single class Student but we can create as many objects as we want (like s1, s2,
etc.)
from that single class.
This is similar to what happens in a factory. Consider a factory which produces car. They
have
only a single design of a car but produce multiple cars from that single design.
Things are similar in the world of classes and objects. There is a single definition of a
particular
class (like Student) but we can produce many Student objects (like s1, s2) from that single
class.
6. State two rules you should follow for naming a class.
Ans. While using names for a class the following set of rules are to be kept in mind.
1. It can have any alphabet (capital or small), digits, underscore and dollar sign characters.
For
example, a, b, cat, mat123, cost_price, Topaz$ are all example of valid identifier.
2. It should not begin with digits or should not contain any special character. For example
2ab,
ab#c, top@, etc., are invalid identifiers as it either begins with a digit or contain special
characters (like #, @).
3. It cannot have a space between it. For example, Simple Interest or Selling Price are
invalid
class-names as it contains a space.
4. It must not be a keyword. For example, for, while, do are invalid class-names as they are
keywords and are assigned some special function for the language.
5. It can be of any length. Even though Java gives you the flexibility to provide a huge length
for
an identifier a very long name is impractical and difficult to manage.
7. What two conventions you should follow naming a class?
Ans. Conventions for class name:
1. Class name should begin with capital letter. Example, Bank, School, Student
2. If a class name consists of more than one word, the first letter of each new word should
be
in uppercase. For example MyClass, AccountDetails, SimpleInterest, etc.

8. State two conventions you should follow while naming a class.


Ans. Class name should begin with capital letter. Example, Bank, School, Student
If a class name consists of more than one word, the first letter of each new word should be
in
uppercase. For example MyClass, AccountDetails, SimpleInterest, etc.
9. Write a statement in Java that will declare an object named si of the SimpleInterest
class.
Ans. SimpleInterest si=new SimpleInterest( );
10. Rewrite the following program after removing the errors, underlining each
corrections:
class My Class
{
int a, b;
void initialize( )
{
a=5;
b=6;
}
void show( )
{
System.out.println(a+ “ ”+ b);
}
static void main( )
{
My Class ob = new My Class( );
ob.initialize( );
show( ).ob;
}
}
Ans.
class MyClass
{
int a, b;
void initialize( )
a=5;
b=6;
}
void show( )
{
System.out.println(a+ “ ” + b);
}
static void main( )
{
MyClass ob = new MyClass( );
ob.initialize( );
ob.show( );
}
}
11. Which among the following are invalid class name in Java? State with reasons.
1. Compound Interest
Ans. Invalid as it contains a space.
2. 1MyClass
Ans. Invalid as it should not begin with a digit.
3. MyClass$
Ans. Valid
4. Myclass#
Ans. Invalid as it contains a special character #.
5. My@Class
Ans. Invalid as it contains a special character @.

You might also like