III.
Short Answers (Java Programming)
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. For example, float y; declares y as
a float type variable.
2. Define variable with an example.
Ans. A variable is a named memory location used to store data. Example: float k = 120;
3. What do you mean by constant?
Ans. Constants are fixed values that do not change.
Examples:
- Integer: 23
- Real: 2.3
- Character: 'g'
- String: "Online"
- Boolean: true, false
- Null:
4. State two kinds of data types.
Ans.
- Primitive: byte, short, int, long, float, double, char, boolean
- Non-Primitive: classes, arrays, interfaces
5. What do you understand by token? Name types.
Ans. Tokens are components of Java statements. Types:
- Keywords
- Identifiers
- Literals
- Operators
- Separators
- Punctuators
- Assignments
6. Rules for assigning a variable in Java:
Ans.
- No space in names
- Cannot start with a number
- Can use letters, digits, _, $
- Names should be meaningful
7. Explain ‘type casting’.
Ans. Converting one data type to another manually is type casting.
Example:
float x = 25.34f;
int n = 30 + (int)x;
8. Perform the following:
a) double pi = 3.142;
b) float y = 1.732f;
9. Distinguish between:
a) Integer vs Floating Constant:
- Integer: whole number (e.g., 10)
- Floating: has decimal (e.g., 10.5)
b) Token vs Identifier:
- Token: keywords, literals, operators, etc.
- Identifier: names for variables, methods, etc.
c) Character vs String Constant:
- Char: 'a'
- String: "a"
d) Character vs Boolean Literal:
- Char: 'x'
- Boolean: true, false
10. Data types:
a) int
b) long
c) float or double
d) char
11. Boolean data type:
Ans. Holds true or false.
Example: boolean flag = true;
12. Primitive data types:
Ans. Predefined types: byte, int, float, char, etc.
13. Why define data type in Java?
Ans. It helps the compiler allocate memory efficiently.
14. Define with example:
a) Implicit:
int a; long b; c = a + b; // a is converted to long
b) Explicit:
double c = 12.3; int x = (int)c;
15. Define ‘Coercion’:
Ans. Automatic type conversion to the higher data type in expressions.
16. Type conversion:
Ans.
- Implicit: automatic
- Explicit: user-defined using casting
17. Static vs Dynamic Declaration:
- Static: int a = 5;
- Dynamic: int a = sc.nextInt();
18. Non-primitive data types:
Ans. Reference types like arrays, classes, interfaces.
19. Return data type:
i) double
ii) double
20. Resultant data types:
Given: int i; float f; double d; char c; byte b;
a) i + c/b → int
b) f/d + c*f → double
c) i + f – b*c → float
d) (f/i)*c + s → float
e) i + f - c + b/d → double
f) i/c + f/b → float