T30 · D2 — Data Types, Type Conversion and Storage Classes
Adapted from
accenture-prep-final/accenture-prep-library/topics/T30-D2-data-types-conversion.md; verified in that library's QA.
1. Why this topic matters here
0.46 expected questions for 48 minutes (ROI 0.460). A coverage gap with no direct local items, included because "storage classes" and "input/output" are explicit aggregator syllabus lines for this section.
The strategic argument is stronger than the weightage suggests: integer division truncation and ASCII arithmetic appear inside pseudocode questions, where a wrong assumption about 7/2 or 'a' - 'A' silently corrupts an entire trace. Studying this topic protects marks in Section B as well as earning them in Section D.
2. Exactly what gets asked
-
Integer division truncation. What
7/2evaluates to in an integer context, and how modulo behaves with negatives. -
Correct: (b) 3. Integer division truncates the fractional part; 7/2 yields 3.
- (c) 3.5 is the real quotient, but both operands are int, so the fraction is discarded.
- (a) 4 is the result of rounding up, which C does not do for integer division.
- (d) 0 would require 7/2 to truncate to zero, which only happens when the numerator is smaller than the denominator.
-
Implicit versus explicit conversion. What is lost when narrowing, and the widening order.
-
Correct: (b) c.
'a'is 97, so 97+2=99, and ASCII 99 is'c'.
- (a)'a'would require adding 0, not 2.
- (d)'b'is ASCII 98, which is'a'+1.
- (c) 99 is the integer value, but%cprints the character, not the number.
-
ASCII arithmetic. That
'A'is 65,'a'is 97,'0'is 48, and'a' - 'A'is 32. -
Correct: (b) static. A
staticlocal variable has static storage duration and keeps its value across calls.
- (a)autois the default for local variables and does not retain value.
- (c)registeris a hint for CPU register storage and does not extend lifetime.
- (d)externdeclares a variable defined elsewhere, not a local with retention.
-
Primitive sizes and ranges. Which type holds what, and default values.
-
Correct: (d) 1. The C standard guarantees
sizeof(char)is exactly 1 byte.
- (a) 2 is wrong; no C standard requirescharto be 2 bytes.
- (c) 4 is a common size forint, notchar.
- (b) While byte size in bits can vary,sizeof(char)is always 1 by definition.
-
Storage classes.
auto,register,static,extern— scope, lifetime and default value. -
Correct: (c) 2.000000.
x/2is integer division (both operands int) giving 2, then converted to float.
- (a) 2.500000 would require writingx / 2.0or(float)x / 2.
- (b)2prints without decimal because%falways shows six decimal places.
- (d) 3.000000 would require rounding, but integer division truncates.
- Pass by value versus reference. Why a naive swap function fails.
3. Concept notes
Primitive vs non-primitive/reference — primitives store the value directly (int, char); references store an address to an object (String, arrays, objects).
Java primitive table
| Type | Bits | Range | Default |
|---|---|---|---|
| byte | 8 | −128 to 127 | 0 |
| short | 16 | −32,768 to 32,767 | 0 |
| int | 32 | −2,147,483,648 to 2,147,483,647 | 0 |
| long | 64 | −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
| float | 32 | ±3.4 × 10³⁸ | 0.0f |
| double | 64 | ±1.8 × 10³⁰⁸ | 0.0d |
| char | 16 | 0 to 65,535 (unsigned Unicode) | '\u0000' |
| boolean | JVM-dependent | true / false | false |
Signed vs unsigned — Java has only signed integers (except char). C/C++ have signed and unsigned.
Integer overflow — exceeding range wraps around. int max + 1 = int min.
Floating-point == — unreliable because of binary representation. 0.1 + 0.2 gives 0.30000000000000004, not 0.3.
Integer division and modulo — integer division truncates toward zero: 7/2 = 3, -7/2 = -3. Modulo result takes the sign of the dividend: -7 % 3 = -1, 7 % -3 = 1.
Widening (implicit) vs narrowing (explicit cast)
| Conversion | Direction | Example | Loss |
|---|---|---|---|
| Widening | Smaller → larger | int → long |
None |
| Narrowing | Larger → smaller | double → int |
Fraction lost; overflow possible |
Widening order: byte → short → int → long → float → double; char → int.
Type promotion — in mixed arithmetic, smaller types promote: if either operand is double → double; else float; else long; else int. byte, short, char promote to int.
char and ASCII — 'A' = 65, 'a' = 97, '0' = 48, 'a' - 'A' = 32.
String vs char — char is a primitive 16-bit Unicode character; String is an immutable object holding a sequence of chars.
Mutable vs immutable — String is immutable; StringBuilder is mutable.
Constants — Java final, C/C++ const, C #define (preprocessor).
Variable scope
| Scope | Declared | Lifetime | Access |
|---|---|---|---|
| Local | Inside method/block | Method/block execution | Within block |
| Instance | Inside class, not static | Object lifetime | Via object |
| Static/class | Inside class with static |
Program lifetime | Via class |
| Global | Outside all functions (C/C++) | Program lifetime | Whole program |
C storage classes
| Storage class | Location | Default value | Scope | Lifetime |
|---|---|---|---|---|
| auto | Stack | Garbage | Local block | Block |
| register | CPU register (if possible) | Garbage | Local block | Block |
| static | Data segment | Zero | File/function | Program |
| extern | Data segment | Zero | Global | Program |
Stack vs heap — stack is fast, automatic, fixed-size, LIFO; heap is slower, manual/dynamic, larger, for objects.
Pass by value vs pass by reference — Java is always pass-by-value (primitives copy value; objects copy the reference). A swap of two int variables cannot work in Java without returning or using an array/object.
4. Must-memorise block
intis 32-bit;long64-bit;char16-bit unsigned;booleanJVM-dependent.0.1 + 0.2 != 0.3; never compare floats with==.- Integer division truncates toward zero; modulo sign follows the dividend.
- Widening is automatic; narrowing needs a cast and may lose data.
- ASCII:
'A'=65,'a'=97,'0'=48, difference 32. - Java is strictly pass-by-value.
- C storage classes:
auto,register,static,extern.
5. Worked examples
Example 1 — Integer division truncates before assignment
int a = 7, b = 2;
double d = a / b;
printf("%.1f", d);
Reasoning:
aandbare bothint, soa / bis integer division.7 / 2evaluates to3, not3.5.- The
intresult3is then converted todoublefor assignment tod. - The fractional part is already lost before the conversion.
Answer: 3.0
Example 2 — Explicit narrowing then implicit widening
int i = 300;
unsigned char c = (unsigned char)i;
int j = c;
printf("%d %d", c, j);
Assume 8-bit unsigned char.
Reasoning:
iholds300.- The explicit cast
(unsigned char)inarrows the value.300 mod 256 = 44, socbecomes44. j = cimplicitly widenscback toint; the value remains44.- In
printf,cis promoted toint, still44.
Answer: 44 44
Example 3 — ASCII arithmetic
char c = 'A' + ('9' - '0');
printf("%c %d", c, c);
Reasoning:
- ASCII:
'A' = 65,'9' = 57,'0' = 48. '9' - '0' = 57 - 48 = 9.'A' + 9 = 65 + 9 = 74.- ASCII
74is'J'.
Answer: J 74
Example 4 — Unsigned overflow wraps modulo 2^8
unsigned char x = 255;
x = x + 1;
printf("%u", x);
Assume 8-bit unsigned char.
Reasoning:
xis255, the maximum value for an 8-bitunsigned char.- In
x + 1,xis promoted toint, so255 + 1 = 256. - Assigning
256back tounsigned charconverts modulo256. 256 mod 256 = 0.- Unsigned conversion/overflow is well-defined in C. Signed overflow is not.
Answer: 0
Example 5 — Static local variable vs global variable
int x = 1;
void f(void) {
static int x = 2;
x++;
printf("%d ", x);
}
int main(void) {
f();
f();
printf("%d", x);
return 0;
}
Reasoning:
- File-scope
xis initialized to1. - Inside
f,static int x = 2shadows the globalx. It has static storage duration and is initialized only once. - First call to
f: local staticxbecomes3, prints3. - Second call to
f: local staticxretains3, becomes4, prints4. - In
main,xrefers to the globalx, which is still1.
Answer: 3 4 1
6. Traps and gotchas
| Trap | Why it catches people | How to avoid |
|---|---|---|
Expecting double d = a / b; to perform floating-point division |
The target type double does not change how the right-hand expression is evaluated. If a and b are int, integer division happens first. |
Check operand types before the operator. Cast at least one operand: (double)a / b. |
Treating char as always signed or always unsigned |
In C, whether plain char is signed is implementation-defined. This changes values above 127 when converted to int. |
Use signed char or unsigned char when the range matters. Cast explicitly before comparisons or arithmetic if needed. |
| Using a digit character as its numeric value | '0' is ASCII 48, not 0. int d = c; for c = '7' gives 55, not 7. |
Convert digit characters with c - '0'. |
| Assuming signed integer overflow wraps around | Signed overflow is undefined behavior in C. The program may not produce a predictable wrap. | Use unsigned types for defined modulo arithmetic. Do not rely on signed wraparound. |
| Missing integer promotions before mixed arithmetic | unsigned char and short are promoted to int before arithmetic. The intermediate result may be int, and overflow/wrap may occur only at assignment or conversion. |
Track promotions: know the type of each subexpression, not just the variable types. |
| Misplacing a cast in a mixed expression | A cast binds tightly. (int)3.9 + 0.1 converts 3.9 first, then adds 0.1. (int)(3.9 + 0.1) converts after the addition. |
Parenthesize the exact subexpression you intend to cast. |
Confusing a static local variable with a global variable |
A static local has block scope but static storage duration. It is initialized once and keeps its value between calls, but it is not visible outside the function. |
Remember: static local = lifetime of program, scope of block. It is not a global. |
Treating extern as a definition |
extern int x; declares that x exists elsewhere. It does not allocate storage. Defining it without extern allocates storage. |
Use extern for cross-file declarations. Define the variable exactly once without extern. |
Assuming local variables are zero-initialized, or that register variables can be addressed |
Automatic variables, including auto and register, are not implicitly zero-initialized. Also, you cannot take the address of a register variable in C. |
Initialize local variables explicitly. Do not use register when you need & or when you expect guaranteed speed. |
7. Practice questions
- [Easy] (D2 — integer division)
What is the output of the following C code?
int a = 7, b = 2;
printf("%d", a / b);
(a) 4 (b) 3 (c) 3.5 (d) 0
- [Easy] (D2 — ASCII arithmetic)
What does this code print?
printf("%c", 'a' + 2);
(a) a (b) c (c) 99 (d) b
-
[Easy] (D2 — storage classes)
Which storage class makes a local variable retain its value between function calls? (a) auto (b) static (c) register (d) extern -
[Easy] (D2 — data types)
In C, what is the value ofsizeof(char)? (a) 2 (b) Depends on the compiler (c) 4 (d) 1 -
[Easy] (D2 — implicit conversion)
What is the output?
int x = 5;
float y = x / 2;
printf("%f", y);
(a) 2.500000 (b) 2 (c) 2.000000 (d) 3.000000
- [Medium] (D2 — unsigned overflow)
What is the output?
unsigned char c = 255;
c = c + 1;
printf("%d", c);
(a) 0 (b) 255 (c) -1 (d) 256
- Correct: (a) 0.
c+1promotes to int 256; assignment tounsigned chartakes modulo 256, giving 0.
- (d) 256 cannot fit in an 8-bitunsigned char.
- (c) -1 is not representable inunsigned char; the value wraps to 0.
- (b) 255 is the starting value, butc+1changes it before assignment.
- [Medium] (D2 — explicit narrowing cast)
Assumecharis a signed 8-bit type. What is the output?
int i = 300;
char c = (char)i;
printf("%d", c);
(a) 44 (b) 300 (c) -44 (d) 12
- Correct: (a) 44. 300 in hex is 0x12C; the lower 8 bits are 0x2C = 44, which is positive.
- (b) 300 is the original int value, not the narrowed char value.
- (c) -44 would require the 8-bit pattern to have the sign bit set, but 0x2C is below 128.
- (d) 12 would be the upper bits or a different truncation, not the lower 8 bits.
-
[Medium] (D2 — storage classes)
Which storage class is a hint to the compiler to store the variable in a CPU register? (a) register (b) extern (c) static (d) auto -
Correct: (a) register. The
registerstorage class suggests the variable be stored in a CPU register for speed.
- (d)autois the default for local variables and gives no register hint.
- (c)staticgives static storage duration, not register storage.
- (b)externdeclares an external variable and has no register hint.
- [Medium] (D2 — ASCII arithmetic)
What is the value ofx?
int x = '9' - '0';
printf("%d", x);
(a) 48 (b) 57 (c) 9 (d) 105
- Correct: (c) 9.
'9'is 57 and'0'is 48; 57-48 = 9.
- (b) 57 is the ASCII value of'9', not the difference.
- (a) 48 is the ASCII value of'0', not the difference.
- (d) 105 is the ASCII value of'i', unrelated to this subtraction.
- [Hard] (D2 — integer division and modulo)
What is the output in C99?
int a = -7, b = 2;
printf("%d %d", a / b, a % b);
(a) -4 -1 (b) -4 1 (c) -3 1 (d) -3 -1
- Correct: (d) -3 -1. C99 integer division truncates toward zero: -7/2 = -3, and remainder takes the dividend's sign: -7%2 = -1.
- (b) -4 1 would be floor division, which C does not use.
- (c) -3 1 has the wrong remainder sign; remainder must match the dividend.
- (a) -4 -1 mixes floor division with a negative remainder, which is inconsistent.
8. Resources
| Resource | Type | Link | Free/Paid | Time | Why this one |
|---|---|---|---|---|---|
| GfG — Bitwise operators | Article | https://www.geeksforgeeks.org/cpp/bitwise-operators-in-c-cpp/ | Free | 20 min | Covers integer representation, overflow and truncation alongside B7 |
| IndiaBix — Computer science Q&A | Practice set | https://www.indiabix.com/computer-science/questions-and-answers/ | Free | 20 min | Recall MCQs on types and storage classes |
| GfG — OOP concepts in Java | Article | https://www.geeksforgeeks.org/java/object-oriented-programming-oops-concept-in-java/ | Free | 15 min | Use the primitives and scope sections |
question-banks/QB-T30-D2-data-types-conversion.md |
Question bank | (this library) | Free | 15 min | 20 original questions |
All external links verified resolving 2026-09-16.
9. Self-check
- [ ] I can state the size, range and default value of each Java primitive
- [ ] I know that integer division truncates and can evaluate
7/2,-7/2and7%2correctly - [ ] I can state the widening conversion order and say what narrowing loses
- [ ] I know that
'A'is 65,'a'is 97,'0'is 48, and'a' - 'A'is 32 - [ ] I can explain why comparing floats with
==is unreliable - [ ] I can describe the four C storage classes on scope, lifetime and default value
- [ ] I can explain why a pass-by-value swap function does not swap the caller's variables
- [ ] I scored 8/10 or better on the practice questions
10. Sources
| Evidence | What it contributed |
|---|---|
| Coverage gap — no local corpus items | Listed in data/analysis-tables.md §6 |
| PrepInsta | Lists "Input/Output" and "Storage Classes" under the pseudocode/CS section |
| Cross-topic dependency | Integer truncation and ASCII arithmetic are prerequisites for Section B traces |
data/weightage.json → subtopic D2 |
est_qs 0.46 · ROI 0.460 · Tier 2 · Medium confidence |