Chapter 5: Concept of Data Types

PrevNextBack

Chapter 5: Concept of Data Types

Contents

Fill in the blanks with an appropriate word/words

Question 1

A character literal is enclosed in single quotes.

Question 2

A set of characters is assigned to a String.

Question 3

The ASCII codes of lowercase letters ranges from 97 to 122.

Question 4

A literal representing True or False comes under boolean data type.

Question 5

11.4f/2.0d is a floating type constant.

Question 6

Class is an example of non primitive data type.

Question 7

A set of characters used together under double quotes is called a string.

Question 8

A word used in a high level language that carries a special meaning for the system compiler is called a keyword.

Question 9

A character literal is assigned to a char variable.

Question 10

In hierarchical arrangement, the higher most data type is double.

Write Short Answers

Question 1

What do you understand by 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.

Question 2

State two kinds of data types.

Answer

Two kinds of data types are:

  1. Primitive Datatypes.
  2. Non-Primitive Datatypes.

Question 3

What do you mean by a variable? Explain with an example.

Answer

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.
Example:
int mathScore = 95;

Question 4

Define constant with an example.

Answer

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;

Question 5

What do you understand by primitive and non primitive data types? Give two examples of each.

Answer

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.

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 6

What do you understand by Token? Name different types of tokens used in Java.

Answer

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

Question 7

What are the points to be taken care while assigning variables in Java programming?

Answer

  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.

Question 8

Differentiate between:

(a) Integer and Floating constant

Answer

Integer ConstantFloating Constant
Integer Constants represent whole number values like 2, -16, 18246, 24041973, etc.Floating Constants represent fractional numbers like 3.14159, -14.08, 42.0, 675.238, etc.
Integer Constants are assigned to variables of data type — byte, short, int, long, charFloating Constants are assigned to variables of data type — float, double

(b) Character and String constant

Answer

Character ConstantString Constant
Character Constants are written by enclosing a character within a pair of single quotes.String Constants are written by enclosing a set of characters within a pair of double quotes.
Character Constants are assigned to variables of type char.String Constants are assigned to variables of type String.

(c) String and Boolean constant

Answer

String ConstantBoolean Constant
String Constants are written by enclosing a set of characters within a pair of double quotes.A boolean constant can take only one of the two boolean values represented by the words true or false.
String Constants are assigned to variables of type String.Boolean literals can only be assigned to variables declared as boolean

(d) Token and Identifier

Answer

TokenIdentifier
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.

(e) Separator and Punctuator

Answer

SeparatorPunctuator
Separators are used to separate the variables or the character.Punctuators are the punctuation signs used as special characters in Java.
Comma, Braces, Curly Brackets, Square brackets are examples of Separators.Question mark, semi colon, dot are examples of Punctuators.

Question 9

Write down the Data type of the following:

(a) Integer

Answer

int

(b) Long Integer

Answer

long

(c) A fractional number

Answer

double

(d) A special character

Answer

char

Question 10

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

Answer

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;

Question 11

Define of the following terms with an example:

(a) Implicit type conversion

Answer

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

Answer

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);

(c) Literals

Answer

Any constant value which can be assigned to the variable is called as literal.

(d) Identifiers

Answer

Identifiers are used to name things like classes, objects, variables, arrays, functions an so on. They are the symbolic representation by which a logical structure is identified.

Question 12

What is the significance of declaring a variable in Java?

Answer

Declaring a variable helps the Java compiler to know the type of value that we want to store in the variable The compiler uses this information to allocate proper memory for storing the variable.

Question 13

What do you mean by initialization of a variable?

Answer

After we declare a variable, its value is unknown. Initialization of a variable means assigning a value to the variable for the very first time after it is declared.

Question 14

In what way is static declaration different from dynamic declaration?

Answer

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 15

What do you mean by non-primitive data type? Give examples.

Answer

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 16

A non-primitive data type is also referred to as reference type. Why?

Answer

Unlike primitive data type, the allocation of non-primitive data type takes place in dynamic memory. The accessing of non-primitive data types is based on their references (addresses). Hence, non-primitive data types are also referred to as reference type.

Question 17

Explain the term type casting?

Answer

The process of converting one predefined type into another is called type casting.

Question 18

Perform the following:

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

Answer

double pi = 3.142;

(b) Assign the value "TEST" to a variable with the requisite data type.

Answer

String str = "TEST";

Predict the return data type in the following expressions

Question 1

int p; double q;
r = p+q;

Answer

    p+q
int + double
double

So the return data type of this expression is double.

Question 2

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

Answer

    m/3*(Math.pow(4,3))
float / int * double
      [∵Math.pow returns double]
float * double
double

So the return data type of this expression is double.

What are the resulting data types, if the following implicit conversions are performed

int i; float f; double d; char c; byte b;

(a) i + c/b;

Answer

     i + c/b;
int + char / byte
int + char
int

(b) f/d + c*f;

Answer

     f/d + c*f;
float / double + char * float
double + float
double

(c) i + f - b*c;

Answer

     i + f - b*c;
int + float - byte * char
int + float - char
float - char
⇒ float

(d) i/c + f/b;

Answer

     i/c + f/b
int / char + float / byte
int + float
float

(e) i + f- c + b/d;

Answer

     i + f- c + b/d;
int + float - char + byte / double
int + float - char + double
float - char + double
float + double
double

What are the resulting data types of the following explicit conversions?

int i; float f; double d; short s; char c; byte b;

(a) (float) i/b + d;

Answer

    (float) i/b + d
float / byte + double
float + double
double

(b) (int)f*d + c/s;

Answer

    (int)f*d + c/s
int * double + char / short
double + short
double

(c) (char)i + f - b*d;

Answer

    (char)i + f - b*d
char + float - byte * double
char + float - double
double

(d) (double) (f/i)*c + s;

Answer

    (double) (f/i)*c + s
(double) (float / int) * char + short
double * char + short
double + short
double

(e) (char) d + b/i - f*s;

Answer

    (char) d + b / i - f * s
char + byte / int - float * short
char + int - float
float