Unit 3
Unit 3
Question 1
1. 2
2. 8
3. 4
4. 16
Answer
Question 2
1. int
2. double
3. char
4. String
Answer
String
Question 3
1. boolean
2. int
3. float
4. 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?
1. 65 - 90
2. 90 - 115
3. 97 - 122
4. 95 - 110
Answer
97 - 122
Question 5
1. \t
2. \\
3. \n
4. ||
Answer
||
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
Question 3
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
Explanation — The data type of the variable that can hold a String literal should also be String.
Question 6
Question 7
Question 8
Question 9
Question 10
Boolean type data is used to test a condition and results in either true or false.
True
Question 1
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:
Question 2
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
(a) variable
(b) constant
(d) coercion
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
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:
1. Identifiers
2. Literals
3. Operators
4. Punctuators
5. Separators
6. Keywords
Question 5
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,
Question 6
(a) m = 22 / 7
(c) k = 0.00004545
(d) n = 24.50
Answer
Question 7
Distinguish between:
Answer
Token Identifier
A token is the smallest element of a Identifiers are used to name things like classes,
program that is meaningful to the compiler. objects, variables, arrays, functions an so on.
Escape Sequences can be used to write character Only true and false values are allowed for
literals 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
(a) char
(b) arrays
(c) int
(d) classes
Answer
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 a = 4;
int b = Math.sqrt(a);
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
Question 12
(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.