Ch. 03 Values and Data Types
Ch. 03 Values and Data Types
(b) Assign the value of √3(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 number Floating Constants represent fractional numbers like
values like 2, -16, 18246, 24041973, etc. 3.14159, -14.08, 42.0, 675.238, etc.
Page 1 of 5
Integer Constant Floating Constant
Integer Constants are assigned to variables of Floating Constants are assigned to variables of data
data type — byte, short, int, long, char type — float, double
A token is the smallest element of a program that is Identifiers are used to name things like classes,
meaningful to the compiler. objects, variables, arrays, functions an so on.
Character Constants are written by enclosing a String Constants are written by enclosing a set of
character within a pair of single quotes. characters within a pair of double quotes.
Character Constants are assigned to variables of String Constants are assigned to variables of type
type char. String.
Escape Sequences can be used to write character Only true and false values are allowed for boolean
literals literals
12. What do you understand by primitive data type? Give two examples.
Ans : 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?
Ans : 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
Ans : 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
Ans : 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.
Ans : 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?
Ans : 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?
Ans : 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;
Page 3 of 5
18. What do you mean by non-primitive data type? Give examples.
Ans : 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);
Ans : Return data type is double.
(ii) float m;
p = m/3*(Math.pow(4,3));
System.out.println(p);
Ans : 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;
Ans : i + c/b;
⇒ int + char / byte
⇒ int + char
⇒ int
(c) i + f - b*c;
Ans : i + f - b*c;
⇒ int + float - byte * char
⇒ int + float - char
⇒ float - char
⇒ float
(d) (f/i)*c + b;
Ans : (f/i)*c + b;
⇒ (float / int) * char + byte
⇒ float * char + byte
⇒ float + byte
⇒ float
(e) i + f- c + b/d;
Ans : i + f- c + b/d;
⇒ int + float - char + byte / double
⇒ int + float - char + double
⇒ float - char + double
Page 4 of 5
⇒ float + double
⇒ double
Page 5 of 5