Unit1 - Subjective Questions
CSE101 • Practice Questions with Detailed Answers
Define the C Character Set. Classify the different characters allowed in C.
C Character Set refers to the set of valid characters that can be used to write a source program in the C language. It is broadly classified into four categories:
- Letters: Uppercase () and Lowercase ().
- Digits: Decimal digits ().
- Special Characters: Symbols used for specific purposes, such as:
- Arithmetic: , , , ,
- Punctuation/Separators: $,$, , , , $.$
- Brackets: , , , , ,
- Others: , , , , , etc.
- White Spaces: Characters that are generally ignored by the compiler but help in formatting, such as Space, Tab (Horizontal , Vertical ), Newline (), and Carriage Return ().
Distinguish between Identifiers and Keywords in C. List the rules for naming identifiers.
Difference:
| Aspect | Keywords | Identifiers |
|---|---|---|
| Definition | Pre-defined words with special meaning reserved by the compiler. | User-defined names given to variables, functions, arrays, etc. |
| Usage | Cannot be used as variable names. | Used to identify program elements. |
| Examples | int, return, if, while. |
sum, total_marks, calcArea. |
Rules for Naming Identifiers:
- Must begin with a letter () or an underscore (_).
- Subsequent characters can be letters, digits (), or underscores.
- They are case-sensitive (
Sumandsumare different). - Keywords cannot be used as identifiers.
- No special characters (like @, $, %) or white spaces are allowed.
Explain the fundamental Data Types in C with their storage size and format specifiers.
C provides four basic (primitive) data types:
-
Integer (
int):- Used to store whole numbers.
- Size: Usually 2 or 4 bytes (16-bit vs 32/64-bit compilers).
- Format Specifier:
%d
-
Character (
char):- Used to store a single character (ASCII value).
- Size: 1 byte.
- Format Specifier:
%c
-
Floating Point (
float):- Used to store single-precision decimal numbers.
- Size: 4 bytes.
- Precision: 6 decimal places.
- Format Specifier:
%f
-
Double Precision (
double):- Used to store double-precision decimal numbers.
- Size: 8 bytes.
- Precision: 15 decimal places.
- Format Specifier:
%lf
Additionally, void is a data type representing the absence of value.
What are Constants in C? Describe the different types of constants with examples.
Constants are fixed values that do not change during the execution of a program.
Types of Constants:
- Integer Constants: Whole numbers. Can be Decimal (
10), Octal (starts with 0, e.g.,012), or Hexadecimal (starts with 0x, e.g.,0xA). - Real (Floating-point) Constants: Numbers with fractional parts (e.g.,
3.14,-24.5) or exponential form (2.5E-3). - Character Constants: A single character enclosed in single quotes (e.g.,
'a','5','+'). - String Constants: A sequence of characters enclosed in double quotes. It ends with a null character
\0automatically (e.g.,"Hello World","C").
Define a Variable. Explain the syntax for declaring and initializing variables with an example.
Definition: A variable is a named storage location in memory used to hold a value that can be modified during program execution.
Declaration Syntax:
This tells the compiler to reserve space for the variable.
Initialization Syntax:
This declares the variable and assigns an initial value.
Example:
c
int age; // Declaration
age = 25; // Assignment
float pi = 3.14; // Declaration and Initialization
char grade = 'A';
Explain Arithmetic Operators in C. Discuss the behavior of the Modulo operator (\%) and Integer Division.
Arithmetic operators perform mathematical operations.
List of Operators:
- (Addition)
- (Subtraction)
- (Multiplication)
- (Division)
- (Modulo/Remainder)
Integer Division Rule:
When both operands of the division operator () are integers, the result is an integer (truncating any fractional part). Example: .
Modulo Operator (\%):
- Returns the remainder of integer division.
- Example: .
- Constraint: The modulo operator cannot be used with floating-point numbers (float or double). It requires integer operands.
Differentiate between Pre-increment () and Post-increment () operators with an example.
Both operators increase the value of the variable by 1, but they differ in when the increment happens within an expression.
-
Pre-increment ():
- Rule: "Change then Use".
- The value of is incremented first, and the new value is used in the expression.
-
Post-increment ():
- Rule: "Use then Change".
- The current value of is used in the expression first, and then is incremented.
Example:
c
int i = 5, a;
a = ++i; // i becomes 6, then a is assigned 6.
int j = 5, b;
b = j++; // b is assigned 5, then j becomes 6.
List the Relational Operators in C. What is the return type of a relational expression?
Relational operators are used to compare two values.
Operators:
- (Equal to)
- (Not equal to)
- (Greater than)
- (Less than)
- (Greater than or equal to)
- (Less than or equal to)
Return Value:
In C, there is no specific boolean type in standard C89/90 (though C99 added stdbool.h). Relational expressions return an integer:
- 1 represents True.
- 0 represents False.
Example: If a = 5 and b = 10, the expression a < b returns 1.
Explain the Logical Operators (AND, OR, NOT) with their Truth Tables. What is short-circuit evaluation?
Logical Operators:
- Logical AND (
&&): Returns true (1) only if both operands are true.- , others are 0.
- Logical OR (
||): Returns true (1) if at least one operand is true.- , others are 1.
- Logical NOT (
!): Reverses the state.- , .
Short-Circuit Evaluation:
- For
&&: If the first operand is False (0), the second operand is not evaluated because the result is already determined to be False. - For
||: If the first operand is True (non-zero), the second operand is not evaluated because the result is already determined to be True.
Describe the Conditional Operator (Ternary Operator). Provide the syntax and a code example to find the maximum of two numbers.
The Conditional Operator is the only ternary operator in C (taking three operands). It acts as a shorthand for if-else statements.
Syntax:
Working:
Expression1(condition) is evaluated.- If it is True (non-zero),
Expression2is executed and becomes the result. - If it is False (zero),
Expression3is executed and becomes the result.
Example (Max of two numbers):
c
int a = 10, b = 20, max;
max = (a > b) ? a : b;
// Result: max will be 20
Explain the Bitwise AND (&), Bitwise OR (|), and Bitwise XOR (^) operators with a numerical example using and .
Bitwise operators perform operations at the bit level.
Given: (Binary: 0101) and (Binary: 0011)
-
Bitwise AND (
&): Result is 1 if both corresponding bits are 1.- $0101$
- $0011$
-
- $0001$ (Decimal: 1)
-
Bitwise OR (
|): Result is 1 if at least one corresponding bit is 1.- $0101$
- $0011$
-
- $0111$ (Decimal: 7)
-
Bitwise XOR (
^): Result is 1 if corresponding bits are different.- $0101$
- $0011$
-
- $0110$ (Decimal: 6)
What are Shift Operators in C? Explain Left Shift and Right Shift with examples. How do they relate to multiplication and division?
Shift operators move bits to the left or right.
-
Left Shift (
<<):- Moves bits to the left, filling vacant positions with 0.
- Effect: Equivalent to multiplying by .
- Example: () = 10.
-
Right Shift (
>>):- Moves bits to the right.
- Effect: Equivalent to dividing by (integer division).
- Example: () = 2.
Mathematical Relation:
What are Assignment and Compound Assignment operators? List five compound assignment operators and expand them.
-
Simple Assignment (
=): Assigns the value of the right operand to the left operand (e.g.,a = 10). -
Compound Assignment: Combines an arithmetic or bitwise operation with assignment. It serves as a shorthand.
Examples:
a += ba = a + ba -= ba = a - ba *= ba = a * ba /= ba = a / ba %= ba = a % ba &= ba = a & b
Compare Implicit Type Conversion (Coercion) and Explicit Type Casting in C expressions.
Implicit Type Conversion (Coercion):
- Performed by: The Compiler automatically.
- When: When operands of different types are mixed in an expression.
- Rule: Smaller types are promoted to larger types (Type Promotion) to prevent data loss (e.g.,
inttofloat). - Example:
int a = 5; float b = a + 2.5;(a is promoted to float implicitly).
Explicit Type Casting:
- Performed by: The Programmer.
- When: When a specific type conversion is required that the compiler might not do automatically, or to force a calculation (like float division between integers).
- Syntax:
(type_name) expression. - Example:
int a = 5, b = 2; float c = (float)a / b;(forces float division).
Explain the concept of Operator Precedence and Associativity. Why are they needed?
Operator Precedence:
Determines the order in which operators are evaluated when an expression contains multiple operators of different types. Operators with higher precedence are evaluated first.
- Example: In
x = 2 + 3 * 4, multiplication () has higher precedence than addition (), so happens first. Result: 14.
Associativity:
Determines the order of evaluation when two operators of the same precedence appear in an expression.
- Left-to-Right: (Most operators)
a - b + cis calculated as(a - b) + c. - Right-to-Left: (Assignment, Unary, Conditional)
a = b = cmeansbgetsc, thenagetsb.
Need: They allow the compiler to uniquely evaluate complex expressions without ambiguity.
Evaluate the following expression step-by-step, assuming int a=10, b=5, c=2;.
Given: a=10, b=5, c=2. Expression: 10 - 5 / 2 + 10 * 2 % 5.
Precedence Order: *, /, % (High, Left-to-Right) +, - (Low, Left-to-Right).
- Division:
5 / 2(Integer division) =2.- Expression:
10 - 2 + 10 * 2 % 5
- Expression:
- Multiplication:
10 * 2=20.- Expression:
10 - 2 + 20 % 5
- Expression:
- Modulo:
20 % 5=0.- Expression:
10 - 2 + 0
- Expression:
- Subtraction:
10 - 2=8.- Expression:
8 + 0
- Expression:
- Addition:
8 + 0=8.
Final Result: 8
What is the sizeof operator? Is it a function or an operator? Explain with an example.
Definition: sizeof is a compile-time unary operator used to calculate the size (in bytes) of a data type or a variable/constant.
Function vs Operator: It looks like a function because of the parentheses, e.g., sizeof(int), but it is a keyword/operator, not a function.
Usage:
sizeof(type)sizeof(variable)
Example:
c
int x;
printf("%d", sizeof(x)); // Output: 4 (on 32/64-bit system)
printf("%d", sizeof(double));// Output: 8
Distinguish between the Assignment Operator (=) and the Equality Operator (==).
| Feature | Assignment Operator (=) |
Equality Operator (==) |
|---|---|---|
| Type | Assignment Operator | Relational Operator |
| Function | Assigns the value of the right operand to the left variable. | Compares two values to check if they are equal. |
| Return Value | Returns the assigned value. | Returns 1 (True) if equal, 0 (False) if not. |
| Example | x = 5; (Sets x to 5) |
if (x == 5) (Checks if x is 5) |
| Common Error | Using = inside if condition usually results in logical errors (always true for non-zero). |
Explain the concept of L-value and R-value in the context of C expressions.
These terms refer to where an expression can appear in an assignment statement.
-
L-value (Locator value):
- Refers to an object that refers to a specific memory location.
- It can appear on the Left (or right) side of an assignment operator.
- Example: Variables are L-values.
a = 10;is valid.
-
R-value (Data value):
- Refers to the data value stored at some address.
- It can only appear on the Right side of an assignment.
- Constants and expressions (like
10orx + y) are R-values. - Example:
10 = a;is Invalid because10is an R-value and does not represent a memory location.
Evaluate the result of x in the following code snippet and explain the logic:
c
int x;
x = 4 + 2 % -8;
Expression: x = 4 + 2 % -8
- Precedence: The Modulo operator (
%) has higher precedence than Addition (+). - Modulo Evaluation (
2 % -8):- In C, the sign of the result of the remainder operator is defined by the sign of the dividend (the left operand).
- Here, dividend is
2(positive). 2 / -8is0with a remainder of2.- So,
2 % -8evaluates to2.
- Addition:
4 + 2=6.
Result: x will be 6.