Values and Data Types
Multiple Choice Questions
1. A constant which gives the exact representation of data is called
2. Variable
3. Literal ✓
4. Identifier
5. Character
2. A word used in a high level language which has a special meaning attached to it is called
1. Class
2. Identifier
3. Keyword ✓
4. Literal
3. A character literal is assigned to a:
1. Char variable ✓
2. Char type literal
3. String variable
4. String literal
4.A character literal is enclosed in:
1. ''✓
2. ""
3. ::
4. {}
5.A set of characters is assigned to:
1. String variable ✓
2. Static variable
3. Boolean variable
4. None
6. The ASCII codes of upper case alphabets range from:
1. 65 - 90 ✓
2. 60 - 85
3. 65 - 91
4. 97 - 122
7.Which of the following results in integer type?
1. 11.4F/3.2D
2. 13.8F/4.6F;
3. 12/3 ✓
4. none
8.Which of the following is non-primitive data?
1. char
2. long
3. object ✓
4. short
9.Which of the following type is an exact representation of fractional values?
1. char
2. double ✓
3. byte
4. String
10 Boolean Data is used to test a particular condition i.e. true or false. Which of the following
is a correct representation?
1. boolean m=true ✓
2. boolean m='true'
3. boolean m="true"
4. none
Fill in the blanks
1. The character sets of Java is like alphabets of English language.
2. A standard encoding system way of representing characters is Unicode.
3. ASCII code is decimal number to represent a character.
4. Each individual component of a Java statement is known as token.
5. In Java, the constants are also called literals.
6. Assignment operator is used to store a value in the variable.
7. The comma, exclamation, question mark etc., are termed as Tokens in Java language.
8. An element of Java program that is used to identify a class, function or value is called
as identifier.
9. Integer type value occupies 4 bytes in the memory.
10. A Java expression that contains all the elements of same data type is pure expression.
Write short answers
1. What do you mean by data type?
Data types are used to identify the type of data a memory location can hold and the associated
operations of handling it.
2. Define variable with an example.
A variable represents a memory location through a symbolic name which holds a known or
unknown value of a particular data type. This name of the variable is used in the program to
refer to the stored value. In the example mathscore is a variable.
Example:
int mathScore = 95;
3. What do you mean by constant? Explain with an example.
The quantities that remain same throughout the execution of a program. Its value can't be
changed in the program. In the example “7” is a constant
Example:
int DAYS_IN_A_WEEK = 7;
4. State two kinds of data types.
Two kinds of data types are:
1. Primitive Datatypes.
2. Non-Primitive Datatypes.
5. What do you understand by Token? Name different types of tokens.
A token is the smallest element of a program that is meaningful to the compiler. The different
types of tokens in Java are:
1. Identifiers
2. Literals
3. Operators
4. Separators
5. Keywords
6.What are the rules to assign a variable in a Java programming?
1. Name of the variable should be a sequence of alphabets, digits, underscore and dollar
sign characters only.
2. It should not start with a digit.
3. It should not be a keyword or a boolean or null literal.
7.Explain the term 'type casting'?
The process of converting one predefined type into another is called type casting.
8. Perform the following:
(a) Assign the value of pie (3.142) to a variable with the requisite data type.
double pi = 3.142;
(b) Assign the value of (1.732) to a variable with the requisite data type.
double x = 1.732;
9. Distinguish between:
(a) Integer and floating constant
Integer Constant Floating Constant
Integer Constants represent whole Floating Constants represent
number values like 2, -16, 18246, fractional numbers like 3.14159, -
24041973, etc. 14.08, 42.0, 675.238, etc.
Integer Constants are assigned to
Floating Constants are assigned to
variables of data type — byte, short, int,
variables of data type — float, double
long, char
(b) Token and Identifier
Token Identifier
A token is the smallest element of a Identifiers are used to name things
program that is meaningful to the like classes, objects, variables, arrays,
compiler. functions an so on.
Tokens in Java are categorised into 5
types — Keywords, Identifiers, Literals, Identifier is a type of token in Java.
Punctuators, Operators.
(c) Character and String constant
Character Constant String Constant
Character Constants are written by String Constants are written by
enclosing a character within a pair of enclosing a set of characters within a
single quotes. pair of double quotes.
Character Constants are assigned to String Constants are assigned to
variables of type char. variables of type String.
(d) Character and Boolean literal
Character Literal Boolean Literal
Character literals are written by A boolean literal can take only one of
enclosing a character within a pair of the two boolean values represented by
single quotes. the words true or false.
Character literals can be assigned to
Boolean literals can only be assigned
variables of any numeric data type —
to variables declared as boolean
byte, short, int, long, float, double, char
Escape Sequences can be used to write Only true and false values are allowed
character literals for boolean literals
10.Write down the data type of the following:
(a) Integer
int
(b) Long Integer
long
(c) A fractional number
double
(d) A special character
char
11.What do you understand by Boolean type data? Explain with an example.
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;
12.What do you understand by primitive data type? Give two examples.
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,
boolean.
13.Why is it necessary to define data type in Java programming?
Data types tells Java how much memory it should reserve for storing the value. Data types
also help in preventing errors as the compiler can check and flag illegal operations at compile
time itself.
14. Define the following with an example each:
(a) Implicit type conversion
In implicit type 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. Example:
int a = 10;float b = 25.5f, c;c = a + b;
(b) Explicit type conversion
In 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);
15. Define 'Coercion' with reference to type conversion.
In a mixed-mode expression, the process of promoting a data type into its higher most data
type available in the expression without any intervention by the user is known as Coercion.
Example:
byte b = 42;int i = 50000;double result = b + i;
16. What do you mean by type conversion? How is implicit conversion different from
explicit conversion?
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);
17. In what way is static declaration different from dynamic declaration?
In static declaration, 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 declaration, the initial value of the variable is the result of an expression or the
return value of a method call. Dynamic declaration 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 18
What do you mean by non-primitive data type? Give examples.
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.
19.Predict the return data type of the following:
(i)
int p; double q;r = p+q;System.out.println(r);
Answer
Return data type is double.
(ii)
float m;p = m/3*(Math.pow(4,3));System.out.println(p);
Answer
Return data type is double.
20.What are the resultant data types if the following implicit conversions are performed?
Show the result with flow lines.
int i; float f; double d; char c; byte b;
(a) i + c/b;
Answer
⇒ int + char / byte
i + c/b;
⇒ int + char
⇒ int
(b) f/d + c*f;
Answer
⇒ float / double + char * float
f/d + c*f;
⇒ double + float
⇒ double
(c) i + f - b*c;
Answer
⇒ int + float - byte * char
i + f - b*c;
⇒ int + float - char
⇒ float - char
⇒ float
(d) (f/i)*c + b;
Answer
⇒ (float / int) * char + byte
(f/i)*c + b;
⇒ float * char + byte
⇒ float + byte
⇒ float
(e) i + f- c + b/d;
Answer
⇒ int + float - char + byte / double
i + f- c + b/d;
⇒ int + float - char + double
⇒ float - char + double
⇒ float + double
⇒ double
(f) i/c + f/b;
Answer
⇒ int / char + float / byte
i/c + f/b
⇒ int + float
⇒ float