0% found this document useful (0 votes)
5 views

Ch. 03 Values and Data Types

The document covers fundamental concepts of data types and variables in Java programming, including definitions of data types, variables, constants, and tokens. It explains type casting, implicit and explicit conversions, and the differences between primitive and non-primitive data types. Additionally, it provides examples and rules for variable assignment, as well as the significance of defining data types in Java.

Uploaded by

vedu yo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ch. 03 Values and Data Types

The document covers fundamental concepts of data types and variables in Java programming, including definitions of data types, variables, constants, and tokens. It explains type casting, implicit and explicit conversions, and the differences between primitive and non-primitive data types. Additionally, it provides examples and rules for variable assignment, as well as the significance of defining data types in Java.

Uploaded by

vedu yo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ch.

03, Values and Data Types


PART B – SUBJECTIVE QUESTIONS
Write short answers
1. What do you mean by data type?
Ans : 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.
Ans : 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;
3. What do you mean by constant? Explain with an example.
Ans : 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;
4. State two kinds of data types.
Ans :Two kinds of data types are:
Primitive Data types.
Non-Primitive Data types.
5. What do you understand by Token? Name different types of tokens.
Ans : A token is the smallest element of a program that is meaningful to the compiler. The different types of
tokens in Java are:
Identifiers
Literals
Operators
Separators
Keywords
6. What are the rules to assign a variable in a Java programming?
Ans : Name of the variable should be a sequence of alphabets, digits, underscore and dollar sign characters
only.
It should not start with a digit.
It should not be a keyword or a boolean or null literal.
7. Explain the term 'type casting'?
Ans : 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 √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

(b) Token and Identifier


Token Identifier

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.

Tokens in Java are categorised into 5 types —


Keywords, Identifiers, Literals, Punctuators, Identifier is a type of token in Java.
Operators.

(c) Character and String constant


Character Constant String Constant

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.

(d) Character and Boolean literal


Character Literal Boolean Literal

A boolean literal can take only one of the two


Character literals are written by enclosing a
boolean values represented by the words true or
character within a pair of single quotes.
false.

Character literals can be assigned to variables of


Boolean literals can only be assigned to variables
any numeric data type — byte, short, int, long,
declared as boolean
float, double, char

Escape Sequences can be used to write character Only true and false values are allowed for boolean
literals 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.
Ans : 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.
Page 2 of 5
Example:
boolean bTest = false;

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

(b) f/d + c*f;


Ans : f/d + c*f;
⇒ float / double + char * float
⇒ double + float
⇒ double

(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

(f) i/c + f/b;


Ans : i/c + f/b
⇒ int / char + float / byte
⇒ int + float
⇒ float

Page 5 of 5

You might also like