CHAPTER – 4
VALUES AND DATA TYPES
PART – A VERY SHORT QUESTIONS
Q1. Find the errors if any in the following statements:
(a) System.out.println("x="; x);
❌ Error: Wrong syntax, semicolon is used instead of +.
✅ Correct:
System.out.println("x=" + x);
(b) System.out.println (“Good Morning”)
❌ Error: Missing semicolon at the end.
✅ Correct:
System.out.println("Good Morning");
(c) System.out.println(“Having fun + x”);
❌ Error: x is inside the quotes, so it will print literally "Having fun + x" instead of
variable value.
✅ Correct:
System.out.println("Having fun " + x);
(d) System.out.println(x + y + )
❌ Error: Extra + at the end.
✅ Correct:
System.out.println(x + y);
(e) Class abc (
Int x ;
)
❌ Error: The ( is directly after class without a corresponding closing parenthesis or curly
braces to define the body.
✅ Correct:
class abc
{
Int x ;
}
Q2. Give an example of char and floating type constants.
char c = 'A';
float f = 3.14f;
Q3. Define the term data type. List the different data types in Java.
Data type defines the type of data a variable can hold.
Types in Java:
o Primitive → byte, short, int, long, float, double, char, boolean
o Non-primitive (reference) → String, arrays, classes, objects, etc.
Q4. How can octal and hexadecimal literals be represented?
Octal → prefix with 0 (zero). Example: 012 (octal for decimal 10).
Hexadecimal → prefix with 0x or 0X. Example: 0xA (decimal 10).
Q5. What do you mean by scope of a variable?
The part of the program where a variable is accessible/usable.
Q6. Determine which of the following are valid literals with reasons and type:
(a) 0.5 → valid, type: double
(b) 9.3E12 → valid, type: double (scientific notation)
(c) 27,822 → invalid (commas not allowed)
(d) 'a' → valid, type: char
(e) '\n' → valid, type: char (escape sequence)
(f) 8.15 PM → invalid (contains letters and space)
(g) 20543 → valid, type: int
Q7. Write a short note on escape sequences in Java.
Escape sequences are special characters prefixed with \ used to represent actions like
newline, tab, etc.
Examples: \n (new line), \t (tab), \" (double quote), \\ (backslash).
Q8. Why is an array called a reference data type?
Because an array variable stores the reference (address) of the memory location, not
the actual elements directly.
Q9. Name the primitive data type in Java that is:
(a) A 64-bit integer → long
(b) A single 16-bit Unicode character → char
Q10. State one difference between floating point literal and double type
literal.
Float literal ends with f or F (e.g., 3.14f).
Double is default for decimals (e.g., 3.14).
Q11. What are the default values of the primitive data type int and float?
int → 0
float → 0.0f
Q12. Identify the literals listed below:
(a) 0.5 → double
(b) 'A' → char
(c) false → boolean
(d) "a" → String
Q13. Arrange the following data types in ascending order of their size:
byte < char < int < double
Q15. Write one difference between primitive data type and composite data
types.
Primitive → stores single value (e.g., int, char).
Composite → group of values/objects (e.g., arrays, classes).
Q16. Perform the following:
(a) double pi = 3.142;
(b) boolean value = true;
Q17. Program to initialize a variable with ASCII code:
public class codeCheck
{
public static void main(String args[])
{
int x = 72;
char y = (char)x;
System.out.println("Char with ASCII code " + x + " is " + y);
}
}
Output:
Char with ASCII code 72 is H
Q18. Program to print output:
public class Hello
{
public static void main(String args[])
{
System.out.println("The Parrot Sang \"Good Morning !!! Friends\"");
}
}
Q19. Program to display all escape sequences in Java:
public class EscapeSeq
{
public static void main(String args[])
{
System.out.println("Newline:\nTab:\tBackslash:\\Double Quote:\"Single Quote:\'");
}
}
Q20. Name the methods of Scanner class that is
(a) used to input an integer data from the standard input stream
(b) used to input a string data from the standard input device
Answer:
(a) nextInt() → used to input an integer value.
(b) nextLine() (or next(), depending on requirement):
nextLine() → reads a whole line of text (string including spaces).
next() → reads a single word (string up to space).
PART – B SHORT QUESTIONS
1. What is a variable?
A variable is a named memory location that stores a value.
👉 Example: int x = 10;
2. What is a constant?
A constant is a fixed value that cannot be changed during program execution.
👉 Declared using final keyword → final int PI = 3.14;
3. What are operators?
Symbols that perform operations on operands.
👉 Categories: Arithmetic, Relational, Logical, Assignment, Unary, Bitwise, Conditional.
4. Short note on punctuators:
Punctuators are special symbols used in Java.
Examples: { } ( ) [ ] ; , .
5. Difference between Z and "Z"?
'Z' → character literal (char).
"Z" → string literal (String object).
6. What are keywords?
Reserved words in Java with predefined meaning (e.g., int, class, if).
👉 Identifiers are user-defined names for variables, classes, methods.
7. Dynamic initialization using Scanner:
👉 Example:
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // value entered at runtime
8. Program to assign value and print:
int a = 10;
a = 500;
System.out.println(a);
9. Program to find volume (L × B × H):
For dimensions 4m × 3m × 2m:
Volume = 4 × 3 × 2 = 24 m³
10. Program for area of glass (2 × (lb + bh + hl)):
Given l=30, b=25, h=25 cm.
Area = 2 × (30×25 + 25×25 + 25×30) = 2 × (750 + 625 + 750) = 2 × 2125 = 4250
cm²
11. Program to add 3 numbers (Scanner):
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
System.out.println(a+b+c);
12. Static method of variable initialization:
When a variable is assigned a value at compile time (not runtime).
👉 Example: int a = 5;
13. What is an array?
A collection of elements of same type stored in contiguous memory.
👉 Syntax: int arr[] = new int[5];
14. Short note on ASCII:
ASCII is a character encoding standard that represents characters using 7 bits (0–127).
15. What is Extended ASCII?
Extended ASCII uses 8 bits → allows 256 characters (0–255).
16. Java statement to read input (Scanner):
Character: char c = sc.next().charAt(0);
String: String s = sc.nextLine();
17. Advantages of Unicode:
Represents all world languages.
Standard across platforms.
Supports 65,536+ characters.
18. Who uses Unicode?
Most modern programming languages, operating systems, browsers, and databases.
19. Program to find sum of 3 numbers (Scanner):
(Same as Q11, but with sum output).
20. Program to accept base & height of triangle and find area, perimeter:
👉 Formula:
Hypotenuse = Math.sqrt(b*b + h*h)
Area = 0.5 * b * h
Perimeter = b + h + hyp
21. Program to find area and perimeter of rectangle:
Area = l × b
Perimeter = 2 × (l + b)
22. Program to input 3 numbers and compute average:
👉 (a + b + c) / 3.0
23. Program to input and swap two numbers (Scanner):
int temp = a;
a = b;
b = temp;
24. What is a literal?
A fixed value directly written in a program.
👉 Example: 10, 'A', "Hello", true.
25. Distinguish between:
(a) Integer constant → e.g., 10; Floating constant → e.g., 10.5
(b) Token → smallest unit (keywords, identifiers, etc.); Identifier → user-defined name.
(c) Character constant → 'A'; String constant → "A".
(d) Character literal → 'X'; Boolean literal → true/false.