0% found this document useful (0 votes)
66 views8 pages

Class As The Basis of All Compuration

Uploaded by

sumedhdarshan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views8 pages

Class As The Basis of All Compuration

Uploaded by

sumedhdarshan5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Class As the Basis of All Computation

Multiple Choice Questions

Question 1

Which keyword makes class members accessible outside the class in which they are declared?

1. Private
2. Protected
3. Public
4. Hidden

Answer

Public

Reason — Public keyword makes class members accessible outside the class in which they are declared.

Question 2

Find the access specifier which prohibits a class member from being used outside a class:

1. Private
2. Public
3. Protected
4. None

Answer

Private

Reason — Private prohibits a class member from being used outside a class.

Question 3

A class object is also known as:

1. Identifier
2. Instance variable
3. Specifier
4. Modifier

Answer

Instance variable

Reason — A class object is also known as instance variable.

Question 4

Which of the following statements is the most appropriate for the private members?

1. They are visible out of the class in which they are defined.
2. They can be used in the sub-classes.
3. They are only visible in the class in which they are declared.
4. None of the above.

Answer

They are only visible in the class in which they are declared.

Reason — Private data members and member methods can only be used within the scope of a class.

Question 5

Which of the following keywords are used to control access to a class member?

1. Default
2. Abstraction
3. Protected
4. Interface

Answer

Protected

Reason — The access specifier 'protected' is used to control access to a class member.

Question 6

Which of the members can be accessed globally?

1. Private
2. Public
3. Protected
4. All of the above

Answer

Public

Reason — Public data members can be accessed globally.

Question 7

The maximum number of objects of a class can be created as:

1. 1
2. 2
3. On the user's choice
4. Number of variables

Answer

On the user's choice

Reason — A user can create any number of objects of a class.

Question 8
A class contains:

1. Attributes and methods


2. A number of object of same types
3. Data and member function
4. All of the above

Answer

All of the above

Reason — A class contains attributes and methods, a number of object of same types, data and member
functions.

Question 9

Which of the following features is not the principle of OOP?

1. Encapsulation
2. Transparency
3. Inheritance
4. Polymorphism

Answer

Transparency

Reason — Transparency is not the principle of OOP.

Question 10

A package is a:

1. collection of data.
2. collection of functions.
3. collection of classes.
4. a nested class.

Answer

collection of classes.

Reason — A package is a collection of classes.

Fill in the blanks with appropriate words

Question 1

Primitive data types are also called as fundamental data types.

Question 2

A user defined data type can be created by using a/an object.

Question 3
this keyword represents the current object in the member method.

Question 4

public members are accessible from anywhere in the program.

Question 5

If no access specifier is mentioned then default specifier is referred by default.

Question 6

private members are accessible only within the same class.

Question 7

protected members are accessible in its own class as well as in a sub class.

Answer the following questions

Question 1

Why is a class known as composite data type?

Answer

A composite data type is one which is composed with various primitive data types. A class can contain various
primitive data types as its data members so it is known as a composite data type.

Question 2

Name the types of data used in a class.

Answer

The types of data used in a class are as follows:

1. Access Specifiers
2. Instance Variables
3. Class Variables
4. Local Variables
5. Constructors
6. Member Methods

Question 3

What is the purpose of the new operator?

Answer

The purpose of new operator is to instantiate an object of the class by dynamically allocating memory for it.

Question 4

Can a class be referred to as user defined data type?


Answer

Yes, a class be referred to as user defined data type since a class is created by the user.

Question 5

What is public access of a class?

Answer

When a class is declared with public access specifier it is said that the class is publicly accessible. A publicly
accessible class is visible everywhere both within and outside its package. For example:

public class Example {


//Class definition
}

Question 6

How are private members of a class different from public members?

Answer

The private members of a class are accessible only within the class in which they are declared while the public
members of the class are accessible both within and outside their class.

Question 7

Mention any two attributes required for class declaration.

Answer

Two attributes required for class declaration are the keyword 'class' and the name of the class.

Question 8

Explain instance variables. Give an example.

Answer

Variables that are declared inside a class without using the keyword 'static' and outside any member methods are
termed instance variables. Each object of the class gets its own copy of instance variables. Consider the example
given below:

class Cuboid {
private double height;
private double width;
private double depth;
private double volume;

public void input(int h, int w, int d) {


height = h;
width = w;
depth = d;
}

public void computeVolume() {


volume = height * width * depth;
System.out.println("Volume = " + volume);
}
}
Here, the data members — height, width, depth and volume are instance variables.

Question 9

Explain any two types of access specifiers.

Answer

Two types of access specifiers are as follows:

1. private — A data member or member method declared as private is only accessible inside the scope of a
class in which it is declared.

2. public — A data member or member method declared as public is accessible inside as well as outside of
the class in which it is declared.

Question 10

What is meant by private visibility of a method?

Answer

A member method of a class declared with private access specifier is said to have private visibility. Only the
member methods of its own class can call this method.

Answer the questions given below (Long answer type)

Question 1

'Object is an instance of a class.' Explain this statement.

Answer

Class is a blueprint of an object. When a class is defined, it doesn't acquire any space in memory, it is only the
attributes that must be common to all the objects of that class. Moreover, when an object of a class is created, it
includes instance variables described within the class. This is the reason why an object is called an instance of a
class.

Question 2

Differentiate between built-in data types and user defined data types.

Answer

Built-In Data Types User Defined Data Types

Built-In Data Types are fundamental data types defined User Defined Data Types are created by the
by Java language specification. user.

Sizes of User Defined data types depend upon


Sizes of Built-In Data Types are fixed.
their constituent members.
Built-In Data Types User Defined Data Types

Built-In Data Types are available in all parts of a Java Availability of User Defined data types depends
program. upon their scope.

User Defined data types are composed of Built-


Built-In Data Types are independent components.
In Data Types.

Question 3

Which of the following declarations are illegal and why?

(a) class abc{...}

(b) public class NumberOfDaysWorked{...}

(c) private int x;

(d) private class abc{...}

(e) default key getkey(...)

Answer

(a) Legal

(b) Legal

(c) Legal

(d) Illegal — only 'public' or no access specifier are allowed for class declaration

(e) Illegal — member method can't be explicitly marked 'default'

Question 4

Why can't every class be termed as user defined data type?

Answer

The classes that contain public static void main(String args[]) method are not considered as user defined
data type. Only the classes that don't contain this method are called user defined data type. The presence
of public static void main(String args[]) method in a class, converts it into a Java application so it is not
considered as a user defined data type.

Question 5

Differentiate between static data members and non-static data members.

Answer

Static Data Members Non-Static Data Members

They are declared using keyword 'static'. They are declared without using keyword 'static'.
Static Data Members Non-Static Data Members

All objects of a class share the same copy of static Each object of the class gets its own copy of non-
data members. static data members.

They can be accessed using the class name or They can be accessed only through an object of the
object. class.

Question 6

Differentiate between private and protected visibility modifiers.

Answer

Private members are only accessible inside the class in which they are defined and they cannot be inherited by
derived classes. Protected members are also only accessible inside the class in which they are defined but they
can be inherited by derived classes.

Question 7

Differentiate between instance variable and class variable.

Answer

Instance Variable Class Variable

They are declared without using keyword 'static'. They are declared using keyword 'static'.

Each object of the class gets its own copy of instance All objects of a class share the same copy of class
variables. variables.

They can be accessed only through an object of the They can be accessed using the class name or
class. object.

You might also like