Class As The Basis of All Compuration
Class As The Basis of All Compuration
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
1. Identifier
2. Instance variable
3. Specifier
4. Modifier
Answer
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
1. Private
2. Public
3. Protected
4. All of the above
Answer
Public
Question 7
1. 1
2. 2
3. On the user's choice
4. Number of variables
Answer
Question 8
A class contains:
Answer
Reason — A class contains attributes and methods, a number of object of same types, data and member
functions.
Question 9
1. Encapsulation
2. Transparency
3. Inheritance
4. Polymorphism
Answer
Transparency
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.
Question 1
Question 2
Question 3
this keyword represents the current object in the member method.
Question 4
Question 5
Question 6
Question 7
protected members are accessible in its own class as well as in a sub class.
Question 1
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
Answer
1. Access Specifiers
2. Instance Variables
3. Class Variables
4. Local Variables
5. Constructors
6. Member Methods
Question 3
Answer
The purpose of new operator is to instantiate an object of the class by dynamically allocating memory for it.
Question 4
Yes, a class be referred to as user defined data type since a class is created by the user.
Question 5
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:
Question 6
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
Answer
Two attributes required for class declaration are the keyword 'class' and the name of the class.
Question 8
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;
Question 9
Answer
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
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.
Question 1
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 are fundamental data types defined User Defined Data Types are created by the
by Java language specification. user.
Built-In Data Types are available in all parts of a Java Availability of User Defined data types depends
program. upon their scope.
Question 3
Answer
(a) Legal
(b) Legal
(c) Legal
(d) Illegal — only 'public' or no access specifier are allowed for class declaration
Question 4
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
Answer
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
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
Answer
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.