You are on page 1of 11

Values and Data Types

I. Multiple Choice Questions:

Tick the correct answer:

1. A constant which gives the exact representation of data is called

a) Variable b) Literal c) Identifier d) Character

2. A word used in a high level language which has a special meaning attached to it is called

a) Class b) Identifier c) Keyword d) Literal

3. A character literal is assigned to a:

a) Char variable b) Char type literal

c) String variable d) String literal

4. A character literal is enclosed in:

a) ‘ ’ b) “ ” c) : : d) { }

5. A set of characters is assigned to:

a) String variable b) Static variable

c) Boolean variable d) None

6. The ASCII codes of uppercase alphabets range from:

a) 65 to 90 b) 60 to 85 c) 65 to 91 d) 97 to 122

7. Which of the following results in integer type?

a) 11.4F/3.2D b) 13.8F/4.6F c) 12/3 d) none

8. Which of the following is non-primitive data?

a) char b) long c) object d) short

9. Which of the following type is an exact representation of fractional values?

a) char b) double c) byte d) String


10. Boolean data is used to test a particular condition i.e. true or false. Which of the following is
a correct representation?

a) boolean m=true b) boolean m= ‘true’

c) boolean m= “true” d) none

Ans.

1. b) Literal

2. c) Keyword

3. a) Char variable

4. a) ‘ ’

5. a) String variable

6. a) 65 to 90

7. c) 12/3

8. c) object

9. b) double

10. a) boolean m=true

II. Fill in the blanks:

1. The character sets of Java is like ________________

2. A standard encoding system way of representing characters is ________________

3. ________________code is a decimal number to represent a character.

4. Each individual component of a Java statement is known as _______________

5. In Java the constants are also called _________________

6. _______________operator is used to store a value in the variable.

7. The comma, exclamation, question mark etc are termed as ______________ in Java language.

8. An element of Java program that is used to identify a class, function or value is called as
_______________
9. Integer type value occupies _________________bytes in the memory.

10. A Java expression that contains all the elements of the same data type is
_____________expression.

Ans.

1. alphabets

2. Unicode

3. ASCII code

4. token

5. literals

6. assignment

7. tokens

8. identifier

9. 4 bytes

10. pure expression [ In pure expressions, all the operands are of the same data type]

III Write short answers:

1. What do you mean by data type?

Ans. Data types specify the size and type of values that can be stored in a variable.The data type of
variables must be specified by the programmer to ensure optimum utilisation of memory space. For
example, when we declare float y, it means that the variable y will store floating type values.

2. Define variable with an example.

Ans. A variable is a named memory location which holds a data value of a particular data type. The
value of a variable can change depending on the circumstances in the program. In Java programming,
variables, class names, function names etc are generally called as identifiers. For example consider the
statement, float k=120; Here k is the variable and it stores the value 120.

3. What do you mean by constant? Explain with an example.

Ans. The quantities which remain fixed(do not change) throughout the discussion of the program are
called constants or literals. Java literals may be classified as follows:

 Integer literals (e.g. 23, 560)


 Real literals( e.g. 2.3, 0.0045, 1.0E-03)
 Character literals(‘p’. ‘g’)
 String literals (“Online”, “class”)
 Boolean literals(true, false)
 Null literals ( \0 which is used to mark the end of the String).

4. State two kinds of data types.

Ans. Java data types can be broadly classified as follows:

 Primitive (byte, short, int, long, float, double, char, boolean)


 byte , short, int and long are known as Numeric Integer types
 float and double are known as Numeric Floating type
 char and boolean are known as Non-Numeric types
 Non-primitive data types ( Classes, Arrays, Interfaces)

5. What do you understand by token? Name different types of tokens.

Ans. Token can be defined as each individual component of a Java statement that carries some
meaning and takes part in the effective execution of the program. The various types of tokens
available in Java are :

 Keywords (e.g. int, case, while etc)


 Identifiers (e.g. x, y, Myclass, sampleMethod)
 Literals (e.g. 5, 66.7, ‘b’, “menu”, false etc)
 Operators (e.g. + , – , * , / , % )
 Separators (e.g. , ( ), { }, [ ] ) – comma, brackets, curly brackets, square brackets etc.
 Punctuators (e.g. ?, . , ; ) – question mark , dot , semicolon
 Assignments (e.g. = ) [Used to store constants in variables. Syntax: data type <variable>=<constant ;
Example: int c=23;]

6. What are the rules to assign a variable in a Java programming?

Ans.

 A variable name must not have space in between.


 A variable name cannot start with a number but it can have numbers in between or at the end.
 A variable may have any number of characters.
 It may contain alphabets, digits, dollar sign and underscore.
 The underscore can be used to separate the words of a variable name.
 Preferably variable names may be meaningful.

7. Explain the term ‘type casting’.

Ans.

It is a type conversion in which one data type gets converted to another data type depending on the
user’s choice. This means that the user demands the system to give the result in the desired data type.
Since it involves user’s intervention, it is known as explicit type conversion or type casting. Type
casting is generally used to convert the higher data type to a lower data type.
Example :

float x=25.34;

int n = 30 + (int) x;

Result of the addition, 30 + x will be in float data type due to implicit conversion. But because of
explicit conversion / type casting x is forced to get converted to int data type. Hence the result is
obtained in int data type.

8. Perform the following:

a) Assign the value of pi (3.142) to a variable with the requisite data type.

b) Assign the value of (1.732) to a variable with the requisite data type.

Ans.

a) double pi = 3.142;

b) float y=1.732;

9. Distinguish between

a) Integer and Floating point constant

b) Token and Identifier

c) Character and String constant

d) Character and Boolean literal

Ans.

a) Integer and Floating point constant

A variable declared as integer type contains a whole number. The number may be a positive or
negative number, but without decimal point whereas a Floating point constant is used to store a
fractional number.

b) Token and Identifier

Token

i) A token can be defined as each individual component of a Java statement that carries some
meaning and takes part in the effective execution of the program. Example: keywords, identifiers,
literals, operators etc.
ii) Every token may not be an identifier.

Identifiers

i) Identifiers (variable names, class names, function names etc) are also called variables in Java. A
variable is a named memory location which holds a data value of a particular data type.

ii) Every identifier is a token.

c) Character and String constant

Character constant

i) char is primitive data type.

ii) A character constant is a single character enclosed within single quotes. e.g. ‘s’, ‘f’ .

String constant

i) String is a class (reference data type).

ii) A String constant may be a single character or set of characters enclosed within double quotes.

e.g. “a”, “marks”

d) Character and Boolean literal

Character literals

i) Character literals can be any alphanumeric character enclosed in single quotes.

ii) Character literals cannot be used in a Java program to check whether a given logical condition is
satisfied or not.

Boolean literals

i) Boolean literals can be either true or false which is not enclosed in any quotes.

ii) Boolean literals can be used in a Java program to check whether a given logical condition is
satisfied or not.

10. Write down the data type of the following:

a) Integer

b) Long Integer

c) A fractional number
d) A special character

Ans.

a) int

b) long

c) float or double can be used

d) char

11. What do you understand by boolean data type? Explain with an example.

Ans. It is a special data type in which a variable contains a constant as either true or false, These are
non-figurative constants. The default value of boolean data type is false. Single or double quotes
should not be used for boolean literals, true/false.

Example: boolean y=true, a=false;

12. What do you understand by primitive data type? Give two examples.

Ans. The data types which are independent of any other data type are known as primitive data types.
They are also called as basic data types or predefined data types or inbuilt data types because the
system developers of Java have defined them.

Example: byte, short, int, long, float, double, char and boolean [write any two]

13. Why is it necessary to define data type in Java programming?

Ans. A compiler contains a phase called storage assignment phase. During this phase, memory is
allocated for different variables used in a program. In order to ensure optimum utilisation of memory
space, it becomes necessary for the programmer to select the appropriate data type for variables.

14. Define the following with an example each:

a) Implicit type conversion

b) Explicit type conversion

Ans.

a) Implicit type conversion

In a mixed type expression , the data type of the result automatically gets converted into the higher
most data type available in the expression without any user intervention. This type of conversion is
known as implicit type conversion or Coercion.
Example

int a;

long b , c;

c = a + b;

In this example, the variable ‘a’ is automatically upgraded to long data type.

b) Explicit type conversion

It is another type conversion in which one data type gets converted to another data type depending
on the user’s choice. This means that the user demands the system to get the result in the desired
data type.

Example:

int a = 10;

double c = 34.257

int y = a + (int)c;

The above expression is evaluated as 10+34 instead of 10+34.257 since the user has explicitly
converted the variable ‘c’ into int data type.

15. Define ‘Coercion’ with reference to type conversion.

Ans.

In a mixed type expression, the data type of the result automatically gets converted into the higher
most data type available in the expression without any user intervention. This type of conversion is
known as implicit type conversion or Coercion.

16. What do you mean by type conversion? How is implicit conversion different from explicit
conversion?

Ans. In a mixed expression, the result must be obtained in a specific data type. For this purpose the
data type needs to be converted into the required type. This is known as type conversion.

In an implicit type conversion, the result of a mixed mode expression is obtained in the higher most
data type of the variables in the expression without any intervention of the user.

e.g.

int a;
float b,c;

c = a+b;

In case of explicit type conversion, the data gets converted to another type as per the user’s choice
and requirement. This means that the user demands the system to get the result in the desired data
type.

e.g.

int a;

float b;

double c;

b = (float) (a * c);

17. In what way is static declaration different from dynamic declaration?

Ans.

Static declaration

i) During static declaration the variable gets initialized at the time when it is declared (before its actual
use in the programming logic)

ii) In static declaration the variable is assigned a direct value.

iii) e.g. double d = 23.29 ; char ch = ‘b’; int p = 12;

Dynamic declaration

i) During dynamic declaration, a variable gets initialised at runtime, i.e. during the execution of the
program.

ii) In dynamic declaration, a variable is assigned the outcome of any arithmetical operation or logical
function.

iii) e.g.

m = ob.nextDouble( );

double d = Math.sqrt (m);

In the above example, the value of d can be initialised only during runtime. Hence it is a dynamic
declaration.
18. What do you mean by non-primitive data type? Give examples.

Ans.

They are basically defined as derived data types and are directly or indirectly dependent on primitive
data types. They store data values with reference to the addresses or locations. So, they are called
‘reference data types’ .Examples: classes, arrays, interfaces etc.

19. Predict the return data type of the following :

i) int p; double q;

r=p+q;

System.out.println(r);

ii)float m;

p=m/3* (Math.pow(4,3));

System.out.println(p);

Ans.

i) double ( since int+double yields double because of implicit conversion)

ii) 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; Ans. int

b) f/d + c*f; Ans. double

c) i + f – b*c Ans. float

d) (f/i)*c +s Ans. float

e) i + f-c +b/d; Ans. double

f) i/c + f/b; Ans. float

*********************

You might also like