CHAPTER 2
Which of the following keywords is used to create an instance of a class?
new
public
class
main
Answer
new
Reason — New keyword is used to create an instance of a class.
Question 2
The ............... is called an instance of a class.
attribute
object
state
features
Answer
object
Reason — The data members declared within a class are known as instance variables.
When an object of a class is created, it includes instance variables described
within the class. Hence, an object is called as an instance of a class.
Question 3
The objects we see around us are called as:
Real world object
Virtual object
Imaginary object
None of the above
Answer
Real world object
Reason — The objects we see around us are called as Real world objects.
Question 4
Which of the following makes a function non-returnable?
public
static
void
new
Answer
void
Reason — The keyword 'void' makes a function non-returnable.
Question 5
Which of the following statements is valid for the objects?
They possess same characteristics and behaviour.
They possess same characteristics but different behaviour.
They possess different characteristics and different behaviour.
They possess different characteristics but common behaviour.
Answer
They possess different characteristics but common behaviour.
Reason — Objects are the entities which possess different characteristics and
common behaviour described within the class.
Fill in the blanks
Question 1
Creating object is the fundamental concept in object oriented programming language.
Question 2
A class is also considered as an object factory.
Question 3
A real world object deals with characteristics and behaviours.
Question 4
The object of a class differs on various characteristics.
Question 5
The characteristics of the real world objects are considered to be the data members
of the software objects.
Question 6
An object of a class is created by using a keyword new.
Question 7
Class is a blueprint of the objects.
Question 8
new keyword is used for dynamic allocation of an object.
Answer the following questions
Question 1
How will you define a software object?
Answer
A software object replaces the characteristics and behaviours of a real world
object with data members and member methods, respectively.
Question 2
Define class and object with an example.
Answer
An object is an entity having a specific identity, specific characteristics and
specific behavior.
A class is a blue print that represents a set of objects that share common
characteristics and behaviour.
Take the example of a house. An architect will have the blueprints for a house.
Those blueprints will be plans that explain exactly what properties the house will
have and how they are all laid out. However, it is just the blueprint, we can't
live in it. Builders will look at the blueprints and use those blueprints to make a
physical house. They can use the same blueprint to make as many houses as they
want. Each house will have the same layout and properties. Each house can
accommodate its own families. So, in this example, the blueprint is the class, the
house is the object and the people living in the house are data stored in the
object's properties.
Question 3
What does the following statement mean?
Employee staff = new Employee ( );
Answer
This statement creates a new object of class Employee. The newly created object is
assigned to a variable named staff which is of Employee type. The object can be
accessed using staff variable.
Question 4
A class is also referred to as 'Object Factory'. Comment.
Answer
A class has the complete description of the data elements the object will contain,
the methods the object can do, the way these data elements and methods can be
accessed. A class is used to create similar objects that possess different
characteristics and common behaviour. Hence, class is called an object factory.
Question 5
Why is a class known as composite data type?
Answer
When a user creates a class, it becomes a data type for his program. This data type
includes different primitive types such as int, float, char, etc. Thus, class is
referred to as a user defined data type or Composite Data Type.
Question 6
A statement is given as:
'Study_Table' is an object of the class 'Furniture'. Write down Java statement for
the same.
Answer
Furniture Study_Table = new Furniture();
Question 7
Class and Objects are inter-related. Explain.
Answer
A Class is used to create various Objects that have different characteristics and
common behaviours. Each object follows all the features defined within a class.
That is why class is also referred to as a blue print or prototype of an object.
This way we can say that they are inter-related.
Question 8
Why is an Object called an 'Instance' of a class? Explain.
Answer
The data members declared within a class are also known as instance variables. When
an object of a class is created, it includes instance variable described within the
class. This is the reason that an object is called an instance of a class.
Question 9
Write a statement to create an object 'Keyboard' of the class 'Computer'.
Answer
Computer Keyboard = new Computer();
Question 10
Mention three characteristics and two methods for the following Classes:
(a) class Mobile Phone
(b) class Bike
(c) class Fruits
(d) class Pen
Answer
(a) class Mobile Phone
Characteristics Methods
colour makeCall()
model receiveCall()
IMEI Number
(b) class Bike
Characteristics Methods
colour startEngine()
model stopEngine()
Registration Number
(c) class Fruits
Characteristics Methods
name buy()
quantity sell()
cost
(d) class Pen
Characteristics Methods
name write()
type refill()
Company
CHAPTER 3
How many bytes a char data type occupies in the memory?
2
8
4
16
Answer
Reason — A char data type occupies 2 bytes in the memory.
Question 2
Which of the following is a non-primitive data type?
int
double
char
String
Answer
String
Reason — String is a non-primitive data type.
Question 3
Which of the following is non-numeric data type?
boolean
int
float
double
Answer
boolean
Reason — boolean data type can store only two values - true or false.
Question 4
Which of the following ASCII code range is applicable for lowercase letters?
65 - 90
90 - 115
97 - 122
95 - 110
Answer
97 - 122
Reason — 97 - 122 is the ASCII code range for lowercase letters.
Question 5
What is not an escape sequence?
\t
\\
\n
||
Answer
||
Reason — || is the symbol for logical OR operator.
State True or False
Question 1
There are 128 set of different characters used in a Java program.
False
Explanation — Java uses Unicode which can represent much more than 128 characters
Question 2
The ASCII codes of upper case letters range from 97 to 122.
False
Explanation — ASCII codes of upper case letters range from 65 to 90.
Question 3
A variable gives the exact representation of data.
False
Explanation — A literal gives exact representation of data. As value of variable
can change, so it cannot give exact representation of data.
Question 4
The data types int, float, char are called non-primitive types.
False
Explanation — The data types int, float, char are called Primitive types.
Question 5
A String literal is assigned to a String variable.
True
Explanation — The data type of the variable that can hold a String literal should
also be String.
Question 6
A character literal is always enclosed in double quotes.
False
Explanation — A character literal is enclosed in single quotes.
Question 7
String constant can be written by using a set of alphanumeric characters.
True
Explanation — A String literal or String constant is defined by enclosing a set of
alphanumeric characters within double quotes.
Question 8
An object is said to be a non-primitive data.
True
Explanation — Class is a non-primitive data type so Objects are non-primitive data.
Question 9
The data type int stores fractional values.
False
Explanation — float or double data type stores fractional values.
Question 10
Boolean type data is used to test a condition and results in either true or false.
True
Explanation — Boolean data type can be either true or false.
Write short answers
Question 1
What is meant by data type? Name two types of data type.
Answer
Data types are used to identify the type of data a memory location can hold and the
associated operations of handling it. Data Types are of two types:
Primitive Data Types
Reference or Composite Data Types
Question 2
Why is it necessary to define data type in Java programming?
Answer
Data types tells Java how much memory it should reserve for storing the data value.
Data types also help in preventing errors as the compiler can check and flag
illegal operations at compile time itself.
Question 3
Define the following with an example:
(a) variable
(b) constant
(c) boolean data type
(d) coercion
(e) primitive data type
(f) non-primitive data type
Answer
(a) Variable — Variables are identifiers that are used to name a data that holds a
value in the memory. The value can change depending upon our requirements in the
program. For Example,
int mathScore = 95;
(b) Constant — The keyword final before a variable declaration makes it a constant.
Its value can't be changed in the program.
Example:
final int DAYS_IN_A_WEEK = 7;
(c) Boolean Data Type — A boolean data type is used to store one of the two boolean
values — true or false. The size of boolean data type is 8 bits or 1 byte.
Example:
boolean bTest = false;
(d) Coercion — In a mixed mode expression, when the data type of the result gets
converted into the higher most data type available in the expression without any
intervention of the user, is known as Implicit Type conversion or Coercion.
Example:
int a = 42;
long b = 50000;
long c = a + b;
Here, a is int, b is long so the result of a + b automatically gets converted into
long and assigned to variable c which is of long type.
(e) Primitive Data Type — Primitive data types are the basic or fundamental data
types used to declare a variable. Examples of primitive data types in Java are
byte, short, int, long, float, double, char and boolean.
(f) Non-Primitive Data Type — A non-primitive data type is one that is derived from
primitive data types. A number of primitive data types are used together to
represent a non-primitive data type. Examples of non-primitive data types in Java
are Class and Array.
Question 4
What is a token? Name different types of tokens.
Answer
A token is defined as each individual component of Java program that carries some
meaning and takes active part in program execution. The different types of tokens
in Java are:
Identifiers
Literals
Operators
Punctuators
Separators
Keywords
Question 5
Explain the term type casting.
Answer
Type casting is a data conversion in which the data type of the result in a mixed
mode expression, gets converted into a specific type as per user's choice. The
choice of data type must be written within braces ( ) before the expression.
For example,
int a; float b; char c;
char d = (char) (a + b * c);
Question 6
Assign the following to a variable with suitable data type.
(a) m = 22 / 7
(b) p = 1.4142135 (value of square root of 2)
(c) k = 0.00004545
(d) n = 24.50
Answer
(a) double m = (22.0 / 7.0);
(b) double p = 1.4142135;
(c) double k = 0.00004545;
(d) float n = 24.50f;
Question 7
Distinguish between:
(a) Token and Identifier
(b) Character and Boolean literal
Answer
(a) Difference between Token and Identifier:
Token Identifier
A token is the smallest element of a program that is meaningful to the compiler.
Identifiers are used to name things like classes, objects, variables, arrays,
functions an so on.
(b) Difference between Character and Boolean literal:
Character literal Boolean literal
Character literals are written by enclosing a character within a pair of single
quotes. A boolean literal can take only one of the two boolean values
represented by the words true or false.
Character literals can be assigned to variables of any numeric data type — byte,
short, int, long, float, double, char Boolean literals can only be assigned to
variables declared as boolean
Escape Sequences can be used to write character literals Only true and false
values are allowed for boolean literals
Question 8
Explain the term type conversion. How is implicit conversion different from
explicit conversion?
Answer
The process of converting one predefined type into another is called type
conversion.
In an implicit conversion, the result of a mixed mode expression is obtained in the
higher most data type of the variables without any intervention by the user. For
example:
int a = 10;
float b = 25.5f, c;
c = a + b;
In case of explicit type conversion, the data gets converted to a type as specified
by the programmer. For example:
int a = 10;
double b = 25.5;
float c = (float)(a + b);
Question 9
Classify the following as primitive or non-primitive data types.
(a) char
(b) arrays
(c) int
(d) classes
Answer
(a) char — Primitive Data Type
(b) arrays — Non-Primitive Data Type
(c) int — Primitive Data Type
(d) classes — Non-Primitive Data Type
Question 10
In what way is static initialization of data type different from dynamic
initialization?
Answer
In static initialization, the initial value of the variable is provided as a
literal at the time of declaration. For example:
int mathScore = 100;
double p = 1.4142135;
char ch = 'A';
In dynamic initialization, the initial value of the variable is the result of an
expression or the return value of a method call. Dynamic initialization happens at
runtime. For example:
int a = 4;
int b = Math.sqrt(a);
double x = 3.14159, y = 1.4142135;
double z = x + y;
Question 11
Predict the return data type of 'r' and 'n' from the snippet:
int p; float m;
r = p+m;
n = m/3*(Math.pow(4,3));
System.out.println(r);
System.out.println(n);
Answer
Return type of r is float and n is double.
Question 12
Give reason whether the following assignments are correct or not.
(a) int m =155;
(b) float f = 0.002654132;
(c) String str = 'Computer';
(d) boolean p = false;
(e) String b = "true";
(f) char ch = "apps";
(g) String st= "Application";
(h) double n = 455.29044125;
Answer
(a) This assignment is correct as 155 is an integer literal and it is assigned to
an int variable m.
(b) This assignment is incorrect as data type of 0.002654132 is double but it is
assigned to a float variable. The correct assignment will be double f =
0.002654132d;
(c) This assignment is incorrect as the String literal Computer is enclosed in
single quotes. It should be in double quotes. The correct assignment will be:
String str = "Computer";
(d) This assignment is correct as false is a valid boolean literal and it is
assigned to a boolean variable.
(e) This assignment is correct as "true" is a string literal not a boolean literal
as it is enclosed in double quotes. It is correctly assigned to a String variable.
(f) This assignment is incorrect as "apps" is a string literal not a character
literal and it is assigned to a variable ch of char data type.
(g) This assignment is correct as "Application" is a string literal and it is
correctly assigned to a String variable.
(h) This assignment is correct as 455.29044125 is a literal of double data type and
it is correctly assigned to a double variable.