Skip to content
Topic guide · Programming

C Programming Basics: Data Types, Operators, printf/scanf and Output Questions · সি প্রোগ্রামিংয়ের মৌলিক বিষয়: ডেটা টাইপ, অপারেটর, printf/scanf ও আউটপুট প্রশ্ন

A C program is built from tokens (keywords, identifiers, constants, strings, operators and punctuators) and stores data in typed variables: char is 1 byte, int is usually 4 bytes, float 4 and double 8, though only char's size is fixed by the C standard. Expressions are evaluated by operator precedence and associativity, integer division truncates toward zero (7 / 2 = 3, −7 / 2 = −3), and mixed types are converted to the wider type. printf and scanf use format specifiers such as %d (int), %c (char), %f (float/double in printf), %lf (double in scanf) and %s (string), and scanf needs the address of each variable (&x).

সি প্রোগ্রাম টোকেন (কিওয়ার্ড, আইডেন্টিফায়ার, কনস্ট্যান্ট, স্ট্রিং, অপারেটর ও পাংচুয়েটর) দিয়ে গঠিত এবং ডেটা রাখে নির্দিষ্ট টাইপের ভেরিয়েবলে: char ১ বাইট, int সাধারণত ৪ বাইট, float ৪ ও double ৮ বাইট, যদিও সি স্ট্যান্ডার্ড কেবল char-এর আকারই নির্দিষ্ট করে দেয়। এক্সপ্রেশনের মান বের হয় অপারেটরের প্রিসিডেন্স ও অ্যাসোসিয়েটিভিটি অনুযায়ী; পূর্ণসংখ্যার ভাগে ভগ্নাংশ বাদ দিয়ে ফল শূন্যের দিকে ছাঁটা হয় (৭ / ২ = ৩, −৭ / ২ = −৩), আর ভিন্ন টাইপ মিশলে ছোট টাইপটি বড় টাইপে রূপান্তরিত হয়। printf ও scanf ফরম্যাট স্পেসিফায়ার ব্যবহার করে, যেমন %d (int), %c (char), printf-এ %f (float/double), scanf-এ %lf (double) ও %s (স্ট্রিং); আর scanf-কে প্রতিটি ভেরিয়েবলের অ্যাড্রেস (&x) দিতে হয়।

Asked in past exams
170
Papers
92

1. The building blocks: tokens, keywords and identifiers

Where C comes from. Dennis Ritchie developed C at Bell Labs in 1972, from the earlier language B. It is often called a middle-level language: it has high-level structures but can also work close to the hardware (pointers, bit operations). The smallest complete program:

#include <stdio.h>   /* header file: declares printf */
int main(void) {     /* execution always starts in main */
    printf("Hello\n"); // a one-line comment (C99 and later)
    return 0;
}

Before it runs, a program passes through four stages: the preprocessor handles lines starting with # (#include, #define), the compiler turns C into assembly, the assembler makes an object file (.o or .obj), and the linker joins it with library code into an executable (.exe on Windows). Comments (/* … */, or // to the end of the line) are removed and never reach the compiled program.

The compiler first breaks a C program into tokens, the smallest units that mean something. There are six kinds:

Token kind Examples
Keywords int, float, if, for, return, static
Identifiers (names you choose) total, main, _count, var_9
Constants (literals) 42, 3.14, 'A', 0x1A
String literals "Hello"
Operators +, =, ==, &&, ++, &
Punctuators (special symbols) ;, ,, (, ), {, }, [, ]

Keywords are reserved words that cannot be used as names. C89/C90 has 32 keywords (auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while). C99 added 5 (37 in all) and C11 added 7 more (44). Exams that say "C has 32 keywords" mean C89. Java has 50 keywords (including the unused goto and const), and Python 3 has 35 (True, False and None among them). sizeof is a keyword and an operator in C, but Java has no sizeof; in Java, objects are created with the new operator.

Identifier rules (C and C++; Java differs only where noted):

  • Use only letters, digits and the underscore _ (Java also allows $).
  • The first character must be a letter or an underscore, never a digit.
  • No spaces or other symbols (#, -, @), and not a keyword.
  • Names are case-sensitive: Total, total and TOTAL are three different names.
Name Valid? Why
var_9 Yes Letters, digit and underscore
_ Yes in C/C++ A single underscore is a legal C name (in Java it is a keyword from Java 9)
9_var No Starts with a digit
No#of-students No # and - are not allowed
float No Keyword
star Yes Not a keyword (only struct, static etc. are)

Worked example 1: count the tokens. How many tokens are in printf("i = %d, &i = %x", i, &i);?

  1. printf (identifier) → 1
  2. ( → 2
  3. "i = %d, &i = %x": a whole string literal is one token, whatever is inside it → 3
  4. , i , → 4, 5, 6
  5. & i → 7, 8
  6. ) ; → 9, 10

Answer: 10 tokens. Spaces are not tokens, and nothing inside quotes is counted separately.

2. Data types and their sizes

A data type fixes how many bytes a variable uses, how its bits are read and which operations are allowed. C's types fall into four groups:

C data types split into basic, derived, user-defined and void types, with examples of each and the modifiers that change the basic types.

Classifications differ: many textbooks put void among the primary types (the usual keyed answer to "void is which type?"), and the C standard itself counts arrays, structures, unions, functions and pointers as derived types.

The C standard fixes only minimum ranges; sizeof(char) is always 1, and the rest depends on the compiler and machine:

Type Standard minimum Typical 32-bit (x86) 64-bit Windows Typical 64-bit Linux printf specifier
char 8 bits 1 byte 1 byte 1 byte %c
short 16 bits 2 2 2 %hd
int 16 bits 4 4 4 %d or %i
long 32 bits 4 4 8 %ld
long long (C99) 64 bits 8 8 8 %lld
float about 6 digits 4 4 4 %f
double about 10 digits (15 in practice) 8 8 8 %f (printf), %lf (scanf)
long double at least double 12 (gcc) 8 (MSVC), 16 (MinGW-w64) 16 %Lf
any pointer – 4 8 8 %p

64-bit Windows keeps long at 4 bytes (the LLP64 model); 64-bit Linux makes it 8 (LP64).

So printf("%zu", sizeof(int)); prints 4 on today's compilers (exam papers often write %d, which works on most 32-bit systems but strictly needs %zu or a cast to int). Old 16-bit compilers such as Turbo C used a 2-byte int, which is why some older keys say 2; when the question gives no system, answer 4.

Ranges follow from the number of bits n: an unsigned type holds 0 to 2ⁿ − 1, and a signed type (two's complement) holds −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.

Bits Signed range Unsigned range
8 (char) −128 to 127 0 to 255
16 (short) −32,768 to 32,767 0 to 65,535
32 (int) −2,147,483,648 to 2,147,483,647 0 to 4,294,967,295

Java fixes every size on every machine: byte 1 byte, short 2, int 4, long 8, float 4, double 8 and char 2 (a UTF-16 code unit); the size of boolean is not specified. Python has no fixed-size integers: an int grows as needed (2**100 prints 1267650600228229401496703205376), and a Python float is a C double.

3. Variables, constants and literals

A variable is a named memory location. int age; declares it, int age = 30; declares and initializes it, and age = 31; assigns a new value.

  • A local variable (inside a function) that is not initialized holds an indeterminate ("garbage") value in C. Global and static variables start at 0.
  • In Java, a local variable must be assigned before use, or the program does not compile; fields of an object get default values (0, false, null). So Test t; System.out.println(t.i); is a compile error ("variable t might not have been initialized"), while Test t = new Test(); prints 0 for an int field.
Point Local variable Global variable
Declared Inside a function or block Outside all functions
Visible Only in that function or block In every function after its declaration
Lifetime While the function runs The whole program run
Default value Garbage (indeterminate) 0

Constants are values that cannot change:

  • const int MAX = 100;: a typed, read-only variable.
  • #define PI 3.14159: a preprocessor macro, replaced as text before compiling (no type, no =, no semicolon).
  • enum color { RED, GREEN, BLUE }; gives named integer constants 0, 1, 2.
  • Java uses final (final int MAX = 100;); Python has no true constants, only the naming custom MAX = 100.

Literals and their types:

Literal Meaning Type
26 Decimal int
032 Octal (leading 0) = 26 int
0x1A Hexadecimal = 26 int
26L, 26U Long, unsigned long, unsigned int
1.414 Floating constant double (not float!)
1.414f or 1.414F Floating constant float
'A' Character constant = 65 int in C (char in C++)
"A" String: 'A' plus '\0' array of 2 char

Because a character is stored as its code, 'A' is 65, 'a' is 97 ('a' - 'A' = 32) and '0' is 48. In C, sizeof('A') is sizeof(int) (4), but in C++ it is 1. Escape sequences: \n new line, \t tab, \0 null character (ends every string), \\ backslash, \' and \" quotes.

4. Operators, precedence and associativity

Group Operators
Arithmetic + - * / %
Relational < <= > >= == != (result 1 or 0 in C)
Logical && || ! (short-circuit)
Bitwise & | ^ ~ << >>
Assignment = += -= *= /= %= and so on
Increment / decrement ++ --
Conditional ?:
Special sizeof, , (comma), & (address of), * (indirection), . and -> (member access)

Precedence says which operator binds first; associativity settles ties between operators of the same level:

Level (high → low) Operators Associativity
1 () [] -> . postfix ++ -- Left to right
2 prefix ++ --, unary + -, ! ~, * (indirection), & (address), sizeof, casts Right to left
3 * / % Left to right
4 + - Left to right
5 << >> Left to right
6 < <= > >= Left to right
7 == != Left to right
8 – 10 &, then ^, then | Left to right
11 – 12 &&, then || Left to right
13 ?: Right to left
14 = += -= … Right to left
15 , Left to right

The rules exams test most:

  • *, / and % share one level, above + and -. So 2 + 3 * 4 - 10 / 2 % 3 = 2 + 12 − (5 % 3) = 2 + 12 − 2 = 12, and 7 / 2 * 2 = 3 × 2 = 6 (left to right).
  • Integer division truncates toward zero (fixed by C99; C89 left negatives to the compiler): 7 / 2 = 3, -7 / 2 = −3. The remainder takes the sign of the dividend: -7 % 2 = −1, 7 % -2 = 1, and 5 % 1 = 0. In C, % works only on integers (use fmod for floating values). Java behaves like C here. Python's // floors instead: -7 // 2 is −4 and -7 % 2 is 1.
  • Assignment is right to left: d = c = 5 sets c, then d, to 5.
  • The comma operator evaluates its operands left to right and gives the value of the right one: int a = (1, 2, 3); sets a to 3. (Without the parentheses the commas would separate declarations instead.)
  • A relational operator gives 1 (true) or 0 (false) in C. So with x = 20, y = 10, z = 5, x > y > z is (20 > 10) > 5 = 1 > 5 = 0. (Python chains comparisons, so 20 > 10 > 5 is True there.)
  • && and || short-circuit: with x = 0 and y = 0, after if (x++ && y++) x is 1 but y is still 0, because x++ gave 0 (false) and y++ never ran.
  • Bitwise with 11 (1011) and 3 (0011): 11 | 3 = 11, 11 & 3 = 3, 11 ^ 3 = 8, ~11 = −12, 11 << 2 = 44, 11 >> 1 = 5. Use | to set a bit and & (with an inverted mask) to reset it. For a non-negative x, x & 7 equals x % 8.
  • XOR swap without a third variable: a ^= b; b ^= a; a ^= b; turns a = 10, b = 20 into 20 10.
  • In Java, char values add as numbers: System.out.print('D' + 'E' + 'F'); prints 207 (68 + 69 + 70). But once a String appears, + joins text: 2 + 3 + "x" + 2 + 3 is 5x23.

A neighbouring C++ fact often asked here: the operators ::, ., .*, ?: and sizeof cannot be overloaded; ++, <<, () and ~ can. Java has no user-defined operator overloading.

5. Type conversion

Implicit conversion happens automatically. Small integer types (char, short) are first promoted to int; then, when two operands differ, the "lower" one is converted to the "higher" one:

A simplified ladder of C's usual arithmetic conversions: in a mixed expression the lower operand is converted up to the type of the higher one.

Explicit conversion (a cast) is written (type) value. Converting a floating value to an integer truncates toward zero: (int)3.99 is 3 and (int)-3.99 is −3 (it never rounds).

Worked example 2: mixed types.

float p = 10.5;
int a = 5 * p + 5.0;
printf("%d\n", a);
  1. 5 * p: the int 5 becomes float, giving 52.5.
  2. 52.5 + 5.0: 5.0 is a double, so the sum is the double 57.5.
  3. Storing 57.5 in an int drops the fraction.

Output: 57.

More conversions that cost marks:

  • With int a = 7, b = 2;: a / b = 3, (double)a / b = 3.5, but (double)(a / b) = 3.0, because the division already happened in integers.
  • Signed meets unsigned: -1 < 1u is 0 (false). The −1 is converted to unsigned int, where it becomes 4,294,967,295.
  • Overflow: unsigned arithmetic wraps around: storing 255 + 1 back in an unsigned char (uc++ or uc = uc + 1) gives 0, although the expression uc + 1 itself is the int 256, because uc is promoted to int first. Overflow of a signed integer (INT_MAX + 1) is undefined behaviour in C. Storing an out-of-range value in a signed type is implementation-defined (gcc turns char c = 127; c++; into −128).
  • Java widens automatically but needs a cast to narrow: int x = 3.5; does not compile. Java defines overflow: byte b = 127; b++; gives −128 and Integer.MAX_VALUE + 1 gives −2147483648.
  • Python: int(3.99) is 3, int(-3.99) is −3, 7 / 2 is always 3.5 (true division) and round(2.5) is 2 (ties round to even).

6. Input and output: printf and scanf

Specifier Used for
%d, %i int (signed decimal)
%u unsigned int
%ld, %lld, %hd long, long long, short
%c One character
%s A string (char array ending in '\0')
%f float or double in printf; float in scanf
%lf double in scanf (also accepted by printf since C99)
%Lf long double
%e, %g %e scientific notation; %g uses %e or %f depending on the exponent and drops trailing zeros
%x/%X, %o Hexadecimal, octal
%p A pointer (address)
%zu size_t, the type of sizeof (C99)
%% A literal % sign

Facts that exams ask:

  • %d and %i are the same in printf. In scanf they differ: %i also reads octal and hex prefixes, so typing 012 gives 10 with %i but 12 with %d, and 0x1A gives 26 with %i.
  • scanf needs addresses: scanf("%d", &x);. An array name is already an address, so char name[10]; scanf("%s", name); takes no &.
  • printf returns the number of characters it printed, and scanf returns the number of items it read successfully (or EOF). So if (printf("0")) i = 5; else i = 3; printf("%d", i); prints 05: printf printed one character and returned 1 (true).
  • A number printed with %c shows the character with that code: printf("%c", 100); prints d, and with int x = 107; char y = 'Q';, printf("%c%d", x, y); prints k81.
  • Width and precision: %5d → 42 (right-aligned in 5 places), %-5d → 42 , %05d → 00042, %.2f of 3.14159 → 3.14, %8.3f of 3.14159 → 3.142 (rounded).
  • getchar()/putchar() read and write one character. gets() was removed in C11 because it cannot limit input; use fgets().
  • Java prints with System.out.println(...) or System.out.printf("%d%n", x) and reads with Scanner (nextInt()); Python uses print(...) and input(), which always returns a string (int(input()) for a number).

7. Increment and decrement

Form Effect Value of the expression
++x (prefix) x increases by 1 first The new value
x++ (postfix) x increases by 1 afterwards The old value
--x, x-- The same, decreasing New / old value

Trace int a = 5, b = a++ * 2, c = ++a * 2; (each initializer is finished before the next starts):

  1. b = a++ * 2 uses the old a (5), so b = 10; then a = 6.
  2. c = ++a * 2 makes a = 7 first, so c = 14.

Result: a = 7, b = 10, c = 14. Other rules:

  • sizeof does not evaluate its operand. int i = 12; int j = sizeof(i++); leaves i at 12; printing i, j gives 12 4.
  • ++ and -- need a variable (an lvalue): 5++ and ++(a + b) do not compile.
  • c + 1 >= c does not change c; c++, c = c + 1 and c += 1 all do.
  • Java: int C = 10; System.out.println(C--); prints 10 (the old value), then C is 9. Python has no ++ operator; write x += 1.

8. Find-the-output questions, and when there is no answer

Most questions in this chapter show a short program and ask for its output. Work in this order:

  1. Write down every variable with its starting value, and its type (watch for char, unsigned and float).
  2. Evaluate each expression by precedence, then associativity; do integer division and % exactly.
  3. Apply side effects (++, --, assignments) at the right moment, and remember short-circuiting.
  4. Copy the format string exactly: a space, a comma or a \n in it appears in the output; a missing space joins numbers.

Worked example 3: exact spacing.

int i = 4;
printf("%d %d", +1, i++);
printf("%d", i++);
  1. +1 is just the number 1 (unary plus), and i++ gives 4, then i becomes 5.
  2. The first printf prints 1 4.
  3. The second prints the old i, 5 (then i becomes 6). Its format has no leading space.

The exact output is 1 45; the keyed option "1 4 5" is that answer written with spaces.

When the C standard gives no answer. If a program modifies a variable twice, or modifies it and also reads it for any purpose other than computing the new value, and the two are unsequenced (no sequence point between them), its behaviour is undefined (C11 6.5p2): the standard allows any result, and different compilers (or optimisation levels) really do differ. i = i + 1 is fine; i = i++ and a[i] = i++ are not. gcc with -Wall warns: "operation on 'i' may be undefined".

Code Status in C What to answer
i++ + ++i Undefined behaviour "Undefined". (In Java it is well defined: left to right, so with i = 5, i = i++ + ++i gives 12.)
printf("%d%d", ++i, i++) Undefined (i changed twice among the arguments) "Undefined". Keys that say 64… for i = 4 assumed the arguments were evaluated right to left.
++z + y - 1 - y + z + x++ Undefined (z is changed and read separately) With x = y = z = 5 (as in the paper), a keyed 16 assumes ++z happens before the later z is read (6 + 5 − 1 − 5 + 6 + 5); the standard does not promise this, and the other order gives 15.
int i = i++; Undefined (reads an uninitialized i and modifies it) "Garbage value / undefined"
x = x++; and a[i] = i++; Undefined (In Java, x = x++ leaves x unchanged.)
f() + g() Unspecified, not undefined: either function may run first "Cannot be predicted" (for example, which of func3(6) - func2(4,5) / func1(1,2,3) is called first)
INT_MAX + 1 Undefined (signed overflow) Unsigned arithmetic wraps; signed does not have to.

In an MCQ, choose "undefined behaviour" or "compiler dependent" whenever it is an option. If it is not, the key almost always assumes one particular compiler's order, so pick the option that order produces and expect the question may be contested.

Two more traps from real papers:

  • print 9//2 is Python 2 syntax. In Python 3 it is a SyntaxError (print is a function: print(9//2) prints 4). Keys usually give 4, reading the question as "what is 9//2".
  • register static int i = 10; does not compile: a declaration may have only one storage-class specifier. And you cannot take the address (&) of a register variable.

Practice · অনুশীলন

  1. What is printed by: int x = 20, y = 10, z = 5; printf("%d", x > y > z);
    Answer
    1. It is evaluated as (x > y) > z = 1 > 5, which is false (0) in C.
  2. Which of these are valid C identifiers: var_9, 9_var, _, No#of-students?
    Answer

    var_9 and _ are valid. 9_var starts with a digit, and No#of-students contains # and -, which are not allowed.

  3. In C and in Python, what are −7 / 2 (Python: −7 // 2) and −7 % 2?
    Answer

    C truncates toward zero: −7 / 2 = −3 and −7 % 2 = −1. Python floors: −7 // 2 = −4 and −7 % 2 = 1.

  4. What does printf("%d %i", 012, 0x1A); print?
    Answer

    10 26. 012 is an octal constant (8 + 2 = 10) and 0x1A is hexadecimal (16 + 10 = 26); %d and %i both print an int in decimal.

  5. int i = 12; int j = sizeof(i++); printf("%d %d", i, j); — what is printed on a system with 4-byte int?
    Answer

    12 4. sizeof only looks at the type of its operand and does not evaluate it, so i++ never runs.

Asked in past exams · আগের পরীক্ষায় যেভাবে এসেছে

Our past papers have 170 questions on this topic (90 MCQ, 80 written) from 92 exams. Here are 12 of them; each links to its full paper.

  1. Which of the following operators cannot be overloaded in C/C++?

    • a)Bitwise right shift assignment
    • b)Address of
    • c)Indirection
    • d)Structure reference (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) Structure reference

    "Structure reference" is the name of the member-access (dot) operator a.b, which selects member b of object a. C++ does not allow . to be overloaded; the other operators that cannot be overloaded are :: (scope resolution), .* (member access through a pointer to member) and ?: (conditional). Note that C has no operator overloading at all, so the question is really about C++.

    Do not confuse it with "structure dereference" a->b, which can be overloaded (smart pointers do it).

    For example, a smart-pointer class can define Account* operator->() so that acct->balance works, but no class can change what acct.balance means.

    Why the other options are wrong

    • (a) Bitwise right shift assignment: >>=, like every compound-assignment operator, can be overloaded, e.g. T& operator>>=(int n);.
    • (b) Address of: Unary & can be overloaded, which is why std::addressof exists to get an object's real address.
    • (c) Indirection: Unary * is routinely overloaded by iterators and smart pointers, e.g. T& operator*();.

    Tip: The ones you can never overload: :: . .* ?:.

    Sources: Operator overloading - cppreference.com; Operators in C and C++ - Wikipedia

  2. The term '%i' is used for _____ data type too.

    • a)char
    • b)int (correct answer)
    • c)none of the above
    • d)float
    Explanation · ব্যাখ্যা

    Correct answer: (b) int

    %i is the second conversion specifier for a signed int, alongside %d (hence "too" in the stem).

    • In printf, %d and %i are identical: both print an int in decimal. printf("%i %d", 42, 42); prints 42 42.
    • In scanf, they differ. %d reads only decimal, while %i detects the base from the prefix: a leading 0 means octal and 0x means hexadecimal. Run on the input 010, %i stores 8 but %d stores 10; on 0x1A, %i stores 26.

    Why the other options are wrong

    • (a) char: A single character uses %c.
    • (c) none of the above: Wrong, because (b) int is correct.
    • (d) float: A float uses %f (or %e/%g).

    Tip: %d = %i for printing; for reading, %i also accepts octal (0…) and hex (0x…).

    Source: printf, fprintf, sprintf, snprintf - cppreference.com
    Source: scanf, fscanf, sscanf - cppreference.com

  3. Which is the correct output?

    int i = 4; printf("%d %d", +1,i++); printf("%d", i++);
    
    • a)4 5 6
    • b)5 7 8
    • c)6 4 6
    • d)1 4 5 (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) 1 4 5

    Start with i = 4.

    1. printf("%d %d", +1, i++);
      • +1 is unary plus applied to 1, so it is just the constant 1.
      • i++ passes the old value 4, then i becomes 5.
      • This prints 1 4.
    2. printf("%d", i++); passes 5, then i becomes 6. It prints 5.

    The full output is 1 45. The second printf adds no space before its number, and the option lists the three numbers as 1 4 5. A test run printed 1 45.

    Each call modifies i only once, and a function call is a sequence point, so this code is well defined. It is not an undefined-behaviour puzzle.

    Why the other options are wrong

    • (a) 4 5 6: This ignores the +1 argument and treats the values as already incremented.
    • (b) 5 7 8: i only ever takes the values 4, 5 and 6, so 7 and 8 cannot appear.
    • (c) 6 4 6: This is the output of a different code, printf("%d%d", ++i, i++). Here there is no ++i.

    Tip: i++ gives the old value, and +1 is simply 1.

    Source: Increment/decrement operators - cppreference.com

  4. In Java, which operator is used to create an object?

    • a)class
    • b)scanf
    • c)print
    • d)None (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) None

    In Java an object is created with the new operator, for example Box b = new Box();. new allocates memory for the object, runs the constructor named after it, and returns a reference to the new object. Since new is not among options (a) to (c), the answer is (d).

    Why the other options are wrong

    • (a) class: A keyword used to declare (define) a class, not to create an instance of it.
    • (b) scanf: A C library input function; it does not exist in Java (Java reads input with classes such as Scanner).
    • (c) print: A method of System.out that writes text to the console; it creates nothing.

    Tip: Object creation in Java = new + a constructor call, e.g. new ClassName().

    Source: Creating Objects (The Java Tutorials)

  5. Which operator can be overloaded?

    • a)++ (correct answer)
    • b)::
    • c). (dot)
    • d)0
    Explanation · ব্যাখ্যা

    Correct answer: (a) ++

    The increment operator can be overloaded for a class in both forms: prefix as T& operator++() and postfix as T operator++(int) (the dummy int parameter marks the postfix form). In a test, a struct holding v = 3 defined N& operator++() { ++v; return *this; }; after ++n, n.v was 4.

    Why the other options are wrong

    • (b) ::: The scope resolution operator is one of the operators C++ never allows to be overloaded.
    • (c) . (dot): Member access is also on the cannot-be-overloaded list (with .* and ?:).
    • (d) 0: This is an integer literal, not an operator, so it cannot be overloaded. (If this option was meant to be the function-call operator (), note that () can be overloaded too, as operator().)

    Tip: Cannot overload: :: . .* ?: sizeof; almost everything else, including ++, --, [], () and <<, can be.

    Source: operator overloading - cppreference.com

  6. In Java, which operator is used to create an object?

    • a)class
    • b)scanf
    • c)print
    • d)new (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) new

    In Java an object is created with the new operator, for example Box b = new Box();. new allocates memory for the object, runs the constructor named after it, and returns a reference to the new object. Since new is not among options (a) to (c), the answer is (d).

    Why the other options are wrong

    • (a) class: A keyword used to declare (define) a class, not to create an instance of it.
    • (b) scanf: A C library input function; it does not exist in Java (Java reads input with classes such as Scanner).
    • (c) print: A method of System.out that writes text to the console; it creates nothing.

    Tip: Object creation in Java = new + a constructor call, e.g. new ClassName().

    Source: Creating Objects (The Java Tutorials)

  7. In C, to retain the values of variables between function calls, we should use:

  8. What is the output of the following C code?

    #include<stdio.h>
    int main(){
        int arr[5] = {1,2,3,4,5};
        int *p = (arr+2);
        printf("%d", p[-1]);
        return 0;
    }
    
  9. In Java, which keyword prevents a method from being overridden?

  10. Which C operator below has right-to-left associativity?

  11. Find Output:

    int fun(int *p) {
        *p = *p + 10;
        return *p;
    }
    int main() {
        int x = 5;
        cout << fun(&x);
        return 0;
    }
    
  12. Find Output:

    int main() {
        int x=3, y=2;
        if (x==3)
            y=2;
        else
            y=3;
        cout<<x<<" "<<y<<endl;
        return 0;
    }
    

Common mistakes · সাধারণ ভুল

  • Giving a number for code like i++ + ++i in C: it is undefined behaviour, so choose "undefined" when the options allow it.
  • Treating 1.414 as a float: an unsuffixed floating constant is a double; write 1.414f for a float.
  • Using %f to read a double with scanf: scanf needs %lf for double (printf accepts %f for both).
  • Putting & before an array name in scanf("%s", name): the array name is already an address.
  • Forgetting that integer division truncates: 7 / 2 is 3 and (double)(7 / 2) is 3.0, not 3.5.
  • Assuming int is exactly 4 bytes by rule: the standard guarantees only 16 bits; 4 bytes is the usual answer for modern compilers.

বাংলায় সংক্ষেপে

  • সি প্রোগ্রাম ছয় ধরনের টোকেনে গঠিত: কিওয়ার্ড, আইডেন্টিফায়ার, কনস্ট্যান্ট, স্ট্রিং লিটারেল, অপারেটর ও পাংচুয়েটর; C89-এ কিওয়ার্ড ৩২টি।
  • আইডেন্টিফায়ার অক্ষর বা আন্ডারস্কোর দিয়ে শুরু হয়, এতে কেবল অক্ষর, অঙ্ক ও _ থাকে এবং বড়-ছোট হাতের অক্ষর আলাদা ধরা হয়।
  • সাধারণ আকার: char ১, short ২, int ৪, float ৪, double ৮ বাইট; কেবল char-এর ১ বাইট স্ট্যান্ডার্ডে নির্দিষ্ট। n বিটের সাইনড রেঞ্জ −২ⁿ⁻¹ থেকে ২ⁿ⁻¹ − ১।
  • পূর্ণসংখ্যার ভাগে ভগ্নাংশ বাদ দিয়ে ফল শূন্যের দিকে ছাঁটা হয় (৭ / ২ = ৩), % কেবল পূর্ণসংখ্যায় চলে, আর float থেকে int-এ রূপান্তরে ভগ্নাংশ বাদ যায়।
  • *, /, % একই স্তরের এবং +, −-এর চেয়ে আগে; ইউনারি, ?: ও অ্যাসাইনমেন্ট অপারেটরের অ্যাসোসিয়েটিভিটি ডান থেকে বামে।
  • printf-এ %d/%i int, %c char, %s স্ট্রিং, %f float/double; scanf-এ double-এর জন্য %lf এবং ভেরিয়েবলের আগে & লাগে।
  • একই ভেরিয়েবল এক এক্সপ্রেশনে দুবার বদলালে (যেমন i++ + ++i) আচরণ আনডিফাইন্ড; এর নির্দিষ্ট কোনো আউটপুট নেই।

Updated 2026-10-18. Written by ARBS Academy; the past exam questions are from public recruitment exams.