DriveReady
Tier 10.8 h to studyMedium confidenceEasyabout 3.9 questions3.5% of paper · 15.5% of section

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

  1. Integer division truncation. What 7/2 evaluates to in an integer context, and how modulo behaves with negatives.

  2. 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.


  1. Implicit versus explicit conversion. What is lost when narrowing, and the widening order.

  2. 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 %c prints the character, not the number.


  1. ASCII arithmetic. That 'A' is 65, 'a' is 97, '0' is 48, and 'a' - 'A' is 32.

  2. Correct: (b) static. A static local variable has static storage duration and keeps its value across calls.
    - (a) auto is the default for local variables and does not retain value.
    - (c) register is a hint for CPU register storage and does not extend lifetime.
    - (d) extern declares a variable defined elsewhere, not a local with retention.


  1. Primitive sizes and ranges. Which type holds what, and default values.

  2. Correct: (d) 1. The C standard guarantees sizeof(char) is exactly 1 byte.
    - (a) 2 is wrong; no C standard requires char to be 2 bytes.
    - (c) 4 is a common size for int, not char.
    - (b) While byte size in bits can vary, sizeof(char) is always 1 by definition.


  1. Storage classes. auto, register, static, extern — scope, lifetime and default value.

  2. Correct: (c) 2.000000. x/2 is integer division (both operands int) giving 2, then converted to float.
    - (a) 2.500000 would require writing x / 2.0 or (float)x / 2.
    - (b) 2 prints without decimal because %f always shows six decimal places.
    - (d) 3.000000 would require rounding, but integer division truncates.


  1. 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

  • int is 32-bit; long 64-bit; char 16-bit unsigned; boolean JVM-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:

  • a and b are both int, so a / b is integer division.
  • 7 / 2 evaluates to 3, not 3.5.
  • The int result 3 is then converted to double for assignment to d.
  • 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:

  • i holds 300.
  • The explicit cast (unsigned char)i narrows the value. 300 mod 256 = 44, so c becomes 44.
  • j = c implicitly widens c back to int; the value remains 44.
  • In printf, c is promoted to int, still 44.

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 74 is '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:

  • x is 255, the maximum value for an 8-bit unsigned char.
  • In x + 1, x is promoted to int, so 255 + 1 = 256.
  • Assigning 256 back to unsigned char converts modulo 256.
  • 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 x is initialized to 1.
  • Inside f, static int x = 2 shadows the global x. It has static storage duration and is initialized only once.
  • First call to f: local static x becomes 3, prints 3.
  • Second call to f: local static x retains 3, becomes 4, prints 4.
  • In main, x refers to the global x, which is still 1.

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

  1. [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

  1. [Easy] (D2 — ASCII arithmetic)
    What does this code print?
printf("%c", 'a' + 2);

(a) a (b) c (c) 99 (d) b

  1. [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

  2. [Easy] (D2 — data types)
    In C, what is the value of sizeof(char)? (a) 2 (b) Depends on the compiler (c) 4 (d) 1

  3. [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

  1. [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

  1. Correct: (a) 0. c+1 promotes to int 256; assignment to unsigned char takes modulo 256, giving 0.
    - (d) 256 cannot fit in an 8-bit unsigned char.
    - (c) -1 is not representable in unsigned char; the value wraps to 0.
    - (b) 255 is the starting value, but c+1 changes it before assignment.

  1. [Medium] (D2 — explicit narrowing cast)
    Assume char is 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

  1. 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.

  1. [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

  2. Correct: (a) register. The register storage class suggests the variable be stored in a CPU register for speed.
    - (d) auto is the default for local variables and gives no register hint.
    - (c) static gives static storage duration, not register storage.
    - (b) extern declares an external variable and has no register hint.


  1. [Medium] (D2 — ASCII arithmetic)
    What is the value of x?
int x = '9' - '0';
printf("%d", x);

(a) 48 (b) 57 (c) 9 (d) 105

  1. 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.

  1. [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


  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/2 and 7%2 correctly
  • [ ] 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