C Mastery
C Mastery
STUDYBOOSTERS
ELEVATE YOUR EXAM GAME
Short Notes
UNIT 1 Basics and introduction to C
• The C character set: C uses ASCII character set to represent characters. This
includes letters, digits, special characters, and control characters.
• Identifiers and keywords: Identifiers are names given to variables, functions, and
other user-defined entities in C. Keywords, on the other hand, are reserved words
that have predefined meanings in the language.
• Data types: C supports several data types, including integer, floating-point,
character, and void. These data types can be combined to form complex data
structures.
• Constants and variables: Constants are values that remain fixed throughout the
program, while variables are values that can be modified during program
execution.
• Expressions: An expression is a combination of variables, constants, and operators
that result in a value. Expressions can be used to perform calculations,
comparisons, and other operations.
• Arithmetic operators: C supports several arithmetic operators, including addition
(+), subtraction (-), multiplication (*), division (/), and modulus (%).
• Unary operators: Unary operators are operators that operate on a single operand.
Examples include the increment (++), decrement (--), and negation (-) operators.
• Relational operators: Relational operators are used to compare two values.
Examples include the less than (<), greater than (>), less than or equal to (<=),
and greater than or equal to (>=) operators.
• Logical operators: Logical operators are used to combine multiple conditions in a
single expression. Examples include the AND (&&), OR (||), and NOT (!) operators.
• Assignment operators: Assignment operators are used to assign a value to a
variable. Examples include the equal to (=) operator and the compound
assignment operators (+=, -=, *=, /=, %=).
• Conditional operator: The conditional operator (?:) is a shorthand way of writing
an if-else statement. It evaluates a condition and returns one of two values,
depending on whether the condition is true or false.
• Bitwise operators: Bitwise operators are used to perform operations on the
individual bits of a number. Examples include the AND (&), OR (|), XOR (^), left
shift (<<), and right shift (>>) operators.
P a g e 1 | 65
P a g e 2 | 65
P a g e 3 | 65
Pointers:
Arrays:
File I/O:
• The FILE structure: C provides the FILE structure for working with files. The FILE
structure contains information about the file, such as its name, mode, and
location in the file.
• Opening and closing files: C provides several functions for opening and closing
files, including fopen(), fclose(), and feof(). fopen() is used to open a file, fclose() is
used to close a file, and feof() is used to check whether the end of the file has
been reached.
P a g e 5 | 65
Strings:
Derived types:
P a g e 7 | 65
vii. What is the value of x after the following code is executed: x = 10; x += 5;
a) 5
b) 10
c) 15
d) 20
ix. What is the value of y after the following code is executed: x = 5; y = (x > 3) ?
10 : 20;
a) 5
b) 10
c) 15
d) 20
Medium
P a g e 8 | 65
P a g e 9 | 65
Hard
iii. What is the difference between a static variable and an automatic variable in
C?
a) A static variable is a variable that retains its value between function calls,
whereas an automatic variable is a variable that is destroyed when the function
ends.
b) A static variable is a variable that is declared with the static keyword, whereas
an automatic variable is a variable that is declared without any keyword.
c) Both static and automatic variables are the same thing in C.
d) None of the above.
P a g e 10 | 65
v. What is the value of x after the following code is executed: x = 5; x = x-- - --x;
a) 0
b) 1
c) 2
d) undefined
vii. What is the value of x after the following code is executed: x = 10; x &= 5;
a) 0
b) 5
c) 10
d) 15
Expected Outcomes
#include <stdio.h>
int main() {
int x = 5;
printf("The value of x is %d", x);
return 0;
}
P a g e 11 | 65
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
printf("The sum of x and y is %d", x + y);
return 0;
}
a) The sum of x and y is 12
b) The sum of x and y is 57
c) The program will not compile
d) The sum of x and y is undefined
#include <stdio.h>
int main() {
float x = 3.14;
printf("The value of x is %f", x);
return 0;
}
a) The value of x is 3.140000
b) The value of x is 3.14
c) The program will not compile
d) The value of x is undefined
#include <stdio.h>
int main() {
int x = 5;
printf("The value of x is %d\n", x);
x = 7;
printf("The value of x is now %d", x);
return 0;
}
P a g e 12 | 65
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
if (x < y) {
printf("x is less than y");
} else {
printf("x is greater than or equal to y");
}
return 0;
}
a) x is less than y
b) x is greater than or equal to y
c) The program will not compile
d) x is less than y\nx is greater than or equal to y
#include <stdio.h>
int main() {
int x = 5;
x += 3;
printf("The value of x is %d", x);
return 0;
}
a) The value of x is 8
b) The value of x is 3
c) The program will not compile
d) The value of x is undefined
#include <stdio.h>
int main() {
int x = 5;
P a g e 13 | 65
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
int z = x & y;
printf("The value of z is %d", z);
return 0;
}
a) The value of z is 1
b) The value of z is 4
c) The program will not compile
d) The value of z is undefined
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
int z = x ^ y;
printf("The value of z is %d", z);
return 0;
}
a) The value of z is 2
b) The value of z is 12
c) The program will not compile
P a g e 14 | 65
#include <stdio.h>
int main() {
int x = 5;
int y = 7;
int z = x | y;
printf("The value of z is %d", z);
return 0;
}
a) The value of z is 7
b) The value of z is 12
c) The program will not compile
d) The value of z is undefined
P a g e 15 | 65
h) Which function is used to read formatted input from the standard input
stream in C?
a) scanf()
b) gets()
c) puts()
d) printf()
Medium
a) Which control structure is more suitable for situations where the number of iterations is
unknown?
a) for loop
b) while loop
c) do-while loop
d) switch case statement
b) Which control structure is used to execute a block of code based on multiple possible
outcomes of a single expression?
a) if-else statement
b) while loop
c) for loop
d) switch case statement
c) Which control structure is used to execute a block of code based on multiple possible
outcomes of different expressions?
a) if-else statement
b) while loop
c) for loop
d) switch case statement
d) Which control structure is used to execute a block of code at least once, regardless of
whether the condition is true or false?
a) if-else statement
b) while loop
c) for loop
d) do-while loop
e) Which control structure is used to jump to a specific labeled statement in the program,
and is generally considered bad practice?
a) break statement
b) continue statement
c) goto statement
d) return statement
g) Which modifier is used to define a variable that is accessible only within the current
block of code?
a) const
b) static
c) extern
d) volatile
h) Which function is used to read unformatted input from the standard input stream in C?
a) scanf()
b) gets()
c) puts()
d) getchar()
i) Which function is used to write unformatted output to the standard output stream in C?
a) scanf()
b) puts()
c) getchar()
d) putchar()
j) Which control structure is used to iterate over each element of an array or sequence?
a) for loop
b) while loop
c) do-while loop
d) switch case statement
Hard
14. perl
15. Copy code
16. int i = 0;
17. while (i < 5) {
18. printf("%d\n", i);
19. i++;
20. }
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 0 1 2 3 4 5
d) Compile error
29. Which control structure is used to execute a block of code based on a condition that is
evaluated at the end of each iteration?
a) for loop
b) while loop
c) do-while loop
d) switch case statement
P a g e 19 | 65
50. Which control structure is used to execute a block of code based on a variable that can
take on multiple possible values?
a) if-else statement
b) while loop
c) for loop
d) switch case statement
51. Which modifier is used to define a variable that is accessible across multiple source files
in a C program?
a) const
b) static
c) extern
d) volatile
a) a is greater than b
b) b is greater than a
c) Compile error
d) Undefined behavior
Expected Outcome
int i = 1;
while (i <= 10) {
printf("%d ", i);
i++;
}
a) 1 2 3 4 5 6 7 8 9 10
b) 123456789
c) 2 4 6 8 10
d) 10 9 8 7 6 5 4 3 2 1
int i;
for (i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
a) 0124
b) 01234
c) 1234
d) 0123
int i;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
P a g e 21 | 65
a) 2 4 6 8 10
b) 13579
c) 1 2 3 4 5 6 7 8 9 10
d) 0 2 4 6 8 10
int x = 2;
switch (x) {
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
default:
printf("Other");
}
a) One
b) Two
c) Three
d) Other
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
a) 1 2 3 4
b) 1 2 3 4 5
c) 1 2 3 4 5 6 7 8 9 10
P a g e 22 | 65
int a = 5, b = 10;
if (a > b) {
printf("a is greater than b");
} else {
printf("a is not greater than b");
}
a) a is greater than b
b) a is not greater than b
c) Compile error
d) Undefined behavior
int i;
for (i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
printf("%d ", i);
if (i == 3) {
break;
}
}
a) 013
b) 0123
c) 01234
d) 1234
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 10);
a) 1 2 3 4 5 6 7 8 9 10
b) 2 4 6 8 10
P a g e 23 | 65
int a = 10;
int *p = &a;
printf("%d", *p);
a) 10
b) 0
c) Compile error
d) Undefined behavior
a) 3.000000
b) 3.333333
c) 3.333334
d) 10.000000
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s!", name);
a) Hello
b) Hell
P a g e 24 | 65
int i;
for (i = 0; i < 5; i++) {
printf("%d ", i);
}
a) 0 1 2 3 4
b) 1 2 3 4 5
c) 1 3 5
d) 0 2 4
1. Which of the following storage classes cannot be used for global variables?
a) auto
b) extern
c) static
d) register
2. Which of the following storage classes allows a variable to be accessed from multiple
source files?
a) auto
b) extern
c) static
d) register
4. Which of the following storage classes allocates memory in the data segment?
a) auto
b) extern
c) static
d) register
6. What is the difference between passing arguments by value and passing arguments by
reference?
a) Passing by value makes a copy of the argument, while passing by reference does
not
b) Passing by reference makes a copy of the argument, while passing by value does
not
c) There is no difference between passing by value and passing by reference
d) Passing by value and passing by reference are not valid ways to pass arguments to
a function
Medium
P a g e 26 | 65
2. Which of the following is a valid way to declare a function that returns an integer and
takes two arguments, both of which are integers?
a) int function(int a, int b);
b) function(int a, int b) int;
c) function(int a, int b) { return int; }
d) int function(int, int);
4. Which of the following is a valid way to declare a recursive function that returns a float
and takes one argument, an integer?
a) float function(int a) { return a; }
b) function(float a, int b) { return b; }
c) float function(int a);
d) function(int a);
7. Which of the following is a math library function that returns the absolute value of a
number?
a) pow()
b) sqrt()
c) ceil()
d) abs()
P a g e 27 | 65
10. Which of the following is a valid way to define a function that takes no arguments and
returns nothing?
a) function()
b) void function()
c) int function()
d) function(void)
Hard
#include <stdio.h>
int main() {
int a = 10;
printf("%d\n", ++a + a++);
return 0;
}
a) 20
b) 21
c) 22
d) Undefined behavior
P a g e 28 | 65
5. Which of the following is a math library function that calculates the sine of an angle in
radians?
a) tan()
b) cosh()
c) asin()
d) sin()
#include <stdio.h>
int function(int n) {
if(n == 0) {
return 0;
}
else {
return n + function(n - 1);
}
}
int main() {
printf("%d\n", function(5));
return 0;
}
a) 5
b) 15
c) 25
d) 120
P a g e 29 | 65
#include <stdio.h>
int x = 10;
void function() {
static int x = 5;
x++;
printf("%d\n", x);
}
int main() {
int x = 20;
function();
function();
function();
printf("%d\n", x);
printf("%d\n", ::x);
return 0;
a) 6 7 8 20 10
b) 5 6 7 20 10
c) 6 7 8 10 20
d) 5 6 7 10 20
Expected Outcomes
#include <stdio.h>
#include <math.h>
int main() {
double x = 2.0;
double y = sqrt(x);
printf("%f\n", y);
P a g e 30 | 65
a) 1.414214
b) 2.000000
c) 3.141593
d) 4.000000
#include <stdio.h>
void function(int x, int y) {
int z = x + y;
printf("%d\n", z);
}
int main() {
function(5, 10);
return 0;
}
a) 5
b) 10
c) 15
d) Undefined behavior
#include <stdio.h>
int main() {
int x = 5;
if(x == 5) {
int y = 10;
printf("%d\n", y);
}
printf("%d\n", y);
return 0;
}
a) 10
b) 5
c) Undefined behavior
d) Compilation error
#include <stdio.h>
void function(int *x) {
P a g e 31 | 65
#include <stdio.h>
int main() {
int x = 5;
{
int x = 10;
printf("%d\n", x);
}
printf("%d\n", x);
return 0;
}
7.
a) 5 10
b) 10 5
c) 10 10
d) Compilation error
#include <stdio.h>
int function(int n) {
if(n == 0) {
return 0;
}
else {
return n + function(n - 1);
}
}
int main() {
printf("%d\n", function(3));
return 0;
P a g e 32 | 65
a) 3
b) 6
c) 9
d) 10
#include <stdio.h>
void function() {
static int x = 5;
x++;
printf("%d\n", x);
}
int main() {
function();
function();
function();
return 0;
}
a) 678
b) 567
c) 666
d) 555
#include <stdio.h>
void function(int x) {
static int y = 0;
y += x;
printf("%d\n", y);
}
int main() {
function(5);
function(10);
function(15);
return 0;
}
a) 5 15 30
b) 5 10 15
c) 5 10 25
P a g e 33 | 65
Easy
3. Which operator is used to access the value at the address stored in a pointer variable?
a) &
b) *
c) %
d) /
P a g e 34 | 65
P a g e 36 | 65
10. What is the difference between bubble sort and insertion sort?
a) Bubble sort is faster than insertion sort
b) Bubble sort compares adjacent elements, while insertion sort inserts elements in
their correct position
c) Bubble sort is only used for sorting arrays of small sizes, while insertion sort can
be used for any size of array
P a g e 37 | 65
Hard
Expected Outcome
P a g e 39 | 65
int x = 10;
int *p;
p = &x;
printf("%d", *p);
a) 10
b) 0
c) 1
d) None of the above
int *p = arr;
a) 1
b) 2
c) 4
d) 5
int *p = arr[0];
a) 1
b) 5
P a g e 40 | 65
int n = sizeof(arr)/sizeof(int);
bubbleSort(arr, n);
a) 12345
b) 54321
c) 31425
d) None of the above
reverse(arr, 5);
a) 12345
b) 54321
c) 43215
d) None of the above
int n = sizeof(arr)/sizeof(arr[0]);
int m = sizeof(arr[0])/sizeof(int);
printf("%d", k);
a) 0
b) 1
c) 4
d) None of the above
selectionSort(p, n);
a) 1 2 3 4 5 6 7 8 9
b) 7 8 9 4 5 6 1 2 3
c) 1 4 7 2 5 8 3
8. What is the expected output of the following code?
int main() {
P a g e 42 | 65
printf("%d", *p + 2);
return 0;
a) 1
b) 2
c) 3
d) 5
int main() {
int *p = a;
printf("%d", *(p+2));
return 0;
a) 1
b) 2
c) 3
d) garbage value
int main() {
int *p = &a[1];
P a g e 43 | 65
return 0;
a) 1
b) 2
c) 3
d) garbage value
int main() {
int (*p)[3] = a;
printf("%d", *(*p+2));
return 0;
a) 1
b) 2
c) 3
d) 4
int main() {
int *p = &a[0];
int *q = &a[2];
printf("%d", q-p);
return 0;
P a g e 44 | 65
a) 0
b) 1
c) 2
d) 3
Easy
ii) Which function is used to free the memory allocated by the malloc() function?
a. free()
b. calloc()
c. realloc()
d. None of the above
iii) What happens if we try to access a pointer that has already been freed?
a. The program crashes
b. The pointer becomes null
c. The pointer still points to the same memory location
d. None of the above
P a g e 45 | 65
vii) Which of the following is used to read data from a file in C language?
a. fread()
b. fwrite()
c. fputc()
d. None of the above
Medium
P a g e 46 | 65
P a g e 47 | 65
Hard
iii) What is the difference between stack and heap memory in C language?
a. Stack memory is allocated at compile-time, while heap memory
is allocated at run-time
b. Stack memory is limited in size, while heap memory is not
P a g e 48 | 65
vi) What is the difference between fread() and fgets() functions in C language?
a. fread() reads a binary file, while fgets() reads a text file
b. fread() reads a fixed number of bytes from a file, while fgets()
reads a line of text from a file
c. fgets() reads a fixed number of bytes from a file, while fread()
reads a line of text from a file
d. None of the above
vii) What is the difference between fprintf() and fputs() functions in C language?
a. fprintf() writes formatted data to a file, while fputs() writes
unformatted data to a file
b. fprintf() writes unformatted data to a file, while fputs() writes
formatted data to a file
c. fprintf() and fputs() are equivalent functions
d. None of the above
viii)What is the difference between binary and text mode when opening a file in C
language?
a. Binary mode can only be used for reading binary files, while text
mode can only be used for reading text files
b. In binary mode, data is read and written in its raw form, while in
text mode, data is converted to and from its textual
representation
P a g e 49 | 65
x) What is the purpose of the fseek() function with SEEK_END option in C language?
a. To move the file pointer to the beginning of a file
b. To move the file pointer to the end of a file
c. To move the file pointer to a specific location in a file
d. None of the above
Expected Outcome
*ptr = 42;
free(ptr);
printf("%d\n", *ptr);
a. 42
b. 0
c. undefined behavior
d. compilation error
char str[100];
printf("%s\n", str);
P a g e 50 | 65
Assuming that the file "file.txt" contains the text "Hello, world!"
a. Hello, world!
b. Hello,
c. world!
d. compilation error
free(arr);
a. 00000
b. 01234
c. undefined behavior
d. compilation error
fclose(fp);
Assuming that the file "file.txt" did not exist before running the code.
a. The file "file.txt" is created and contains the text "Hello, world!"
b. The file "file.txt" is created but it is empty
c. undefined behavior
d. compilation error
arr[i] = i;
free(arr);
a. 01200
b. 012
c. undefined behavior
d. compilation error
fclose(fp);
fp = fopen("file.bin", "rb");
int buffer[3];
fclose(fp);
a. 123
b. 321
c. undefined behavior
d. compilation error
*ptr = 42;
printf("%p\n", ptr);
printf("%p\n", ptr);
free(ptr);
fclose(fp);
fp = fopen("file.txt", "r");
char buffer[100];
printf("%s\n", buffer);
fclose(fp);
P a g e 53 | 65
continue writing
ChatGPT
a. Goodbye!
b. Hello, world!Goodbye!
c. Goodbye!Hello, world!
d. compilation error
*ptr = 42;
free(ptr2);
printf("%d\n", *ptr);
a. 42
b. 0
c. undefined behavior
d. compilation error
int n = strlen(str);
fputc(str[i], fp);
fclose(fp);
P a g e 54 | 65
char buffer[100];
printf("%s\n", buffer);
fclose(fp);
Assuming that the file "file.txt" did not exist before running the code.
a. The file "file.txt" is created and contains the text "Hello, world!"
b. The file "file.txt" is created but it is empty
c. undefined behavior
d. compilation error
Easy
a) Hello
b) ello
c) H
d) llo
a) 4
b) 8
c) 2
d) 6
Medium
strcat(str1, str2);
printf("%s", str1);
a) HelloWorld
b) Hello World
c) WorldHello
d) HWorldello
struct myStruct {
int x;
float y;
};
P a g e 58 | 65
a) 5 3.14
b) 3.14 5
c) 55
d) 3.14 3.14
union myUnion {
int x;
float y;
};
union myUnion u;
u.x = 10;
printf("%f", u.y);
a) 10.0
b) 0.0
c) -10.0
d) Depends on the compiler
P a g e 59 | 65
Hard
struct myStruct {
int x;
};
printf("%d", ptr[1].x);
a) 1
b) 2
c) 3
d) Compilation error
union myUnion {
int x;
float y;
};
union myUnion u;
P a g e 60 | 65
a) 10 10.5
b) 1092616192 10.5
c) 10.5 10
d) Depends on the compiler
a) Hel
b) lo
c) Hello
d) Runtime error
struct myStruct {
int x;
P a g e 61 | 65
printf("%d", arr[1].x);
a) 1
b) 2
c) 3
d) Compilation error
struct myStruct {
int x;
};
ptr->x = 10;
printf("%d", ptr->x);
a) 10
b) 0
c) Runtime error
d) Compilation error
Expected Outcome
printf("%d", len);
a) 12
b) 11
c) 13
d) 10
b. Which of the following is the correct way to initialize a structure at the time of
declaration?
a) struct myStruct s = {1, 2.5};
b) struct myStruct s = {1};
c) struct myStruct s = {.y = 2.5};
d) struct myStruct s; s.x = 1; s.y = 2.5;
strcat(str1, str2);
printf("%s", str1);
a) Hello
b) World
c) HelloWorld
d) Runtime error
struct myStruct {
int x;
};
ptr->x = 10;
printf("%d", ptr->x);
free(ptr);
a) 0
b) 10
c) Runtime error
d) Undefined behavior
a) H
P a g e 64 | 65
struct myStruct {
int x;
};
printf("%d", ptr->x);
a) 1
b) 2
c) 3
d) Compilation error
P a g e 65 | 65