C Workbook
C Workbook
INTRODUCTION
1. Fill in the statement below about the history of C with the correct answer:
Programming Language to do
Programmer
A computer language program used to develop
applications, scripts, or other set of instructions for a
computer to execute.
2
4. Match the following.
• Machine dependent
High level
b.
c.
3
6. Match the following:
Modular
programming A logical construct that allows for the efficient operation of a
program.
Object- oriented
programming A method for designing software by way of breaking up
components of a large software program into manageable pieces.
7. Fill in the statement about algorithm below with the correct answer:
[pseudocode, output, Raw egg , Cook the egg on the pan with a little oil,
flowchart , process , input, Fried egg, solve a problem, instruction]
4
C Programming Workbook
Topic 1: Characters Used in C
printf( );
Hello
World
printf("Hello\n ");
int main()
return 0 }
5
7. Use escape characters to print: C:\new\file.txt
printf( );
#include ?
int main()
return 0;
printf("100%% Complete");
11. Output?
printf("Line1\nLine2");
12. Output?
printf("Price: Rs.\t500");
6
13. Output?
a) @ b) # c) ^ d) ~
a) // b) /* c) ** d) --
18. The character \t is used to insert a tab space in output. (True / False)
7
C Programming Workbook
Topic 2: Variables
Section A: Fill in the blanks (5 Questions)
6. Declare an integer variable named `age` and assign it the value 20.
9. Complete the code to swap values of `a` and `b` using a temporary variable.
10. Write a line of code to declare a `const` integer variable `max` with value 100.
8
9
Section C: Predict the output (5 Questions)
printf("%d", a + b);
int x;
printf("%d", x);
// x is uninitialized
13. Output?
int a = 5;
Define Const.
a = a + 10;
printf("%d", a);
14. Output?
pi = 3.14159;
printf("%f", pi);
15. Output?
int a = 3, b = 4;
10
Section D: Quiz – Multiple Choice Questions (3 Questions)
18. What will happen if you use a variable without declaring it?
20. You can change the value of a variable declared as `const`. (True / False)
11
C Workbook: Operators and Expressions
1. Fill in the Blanks
int a = 4, b = 5;
int result = ;
printf("%d", result);
12
printf("Condition met");
int x = 10;
x 3;
printf("%d", x);
int num = 8;
if (num 2 == 0) {
printf("Even");
if (marks 50) {
printf("Pass");
13
3. Predict the Output
int x = 5;
printf("%d", x++);
printf("%d", ++x);
int a = 10, b = 5;
int a = 7;
int b = a % 3;
printf("%d", b);
int x = 4;
x *= 2 + 1;
printf("%d", x);
int a = 1, b = 0;
14
4. Quiz (Multiple Choice Questions)
21. Which of the following is the correct operator for bitwise AND?
a) || b) | c) or d) &&
a) 18 b) 14 c) 13 d) 12
25. What does the expression `!(1 && 0)` evaluate to?
a) 0 b) 1 c) -1 d) Syntax error
a) + b) - c) ++ d) *
a) a = a + b b) a = b c) b = a + b d) a = a - b
a) 0 b) 1 c) 2 d) Error
30. The operator with the lowest precedence among these is:
a) * b) + c) = d) %
15
C Workbook: Conditional Statements
1. Complete the Partial Code
1. Complete the code to check if a number is positive:
printf("Negative");
int day = 2;
switch(_____)
{
case 1:
printf("Sunday");
break;
2:
printf("Monday");
break;
}
16
5. Fill in the condition for checking if a number is even:
int x = 8;
if (x % 2 0)
printf("Even");
int opt = 3;
switch(opt) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
: printf("Other");
}
17
11. Complete the nested if:
int a = 4, b = 4;
if (a == b)
if ( )
printf("Equal and Even");
if ( && )
printf("A is largest");
if (b > c)
printf("B is largest");
else
printf("C is largest");
int num = 7;
if ( )
printf("Even");
else
printf("Odd");
switch(x) {
case 1: printf("One"); ;
default: printf("Default");
}
if ( __________________________________________)
printf("Two-digit number");
int x = -1;
if ( )
printf("Negative");
18
2. Debug the Code
1. Find and fix the error:
if x > 0
printf("Positive");
2. What will be the output and why
int a = 5;
if (a = 10)
printf("Equal");
3. Identify the mistake:
int a = 3;
if (a > 2);
printf("Valid");
4. Debug this code:
int num = 1;
switch(num)
case 1: printf("One"); break;
5. Correct the switch structure:
switch(x) {
printf("Hello");
case 1: break;
}
6. Fix the missing braces:
int x = 2;
if (x < 5)
printf("Yes");
printf("Again");
7. Debug incorrect logical operator:
if (a > 5 and b < 10)
printf("OK");
8. Correct use of default:
switch(a) {
default printf("None");
}
19
9. Fix indentation and logic:
if (x > 0)
if (x < 10)
printf("Single digit");
else
printf("Too high");
10. Find and correct:
int age = 18;
if (age => 18)
printf("Adult");
11. Identify the mistake:
int a=1;
switch(a) {
case 1
printf("One"); break;
}
20
5. Predict the output: 6. What is printed?
int x = 0;
int num = 3; if (x)
if (num > 0) printf("True");
if (num < 5) else
printf("Valid"); printf("False");
else
8. Output?
7. Result of:
int val = 10;
int a = 3; if (val != 10)
if (a == 3) printf("No Match");
printf("Three"); else
printf("Match");
switch(2) { int x = 1;
case 1: if (x > 0)
case 2: printf("Yes");
printf("Matched"); printf("!");
break;
21
15. write a program that prints the numbers from 1 to 100. But for multiples of three print
“Fizz” instead of the number and for the multiples of five print “Buzz“. For numbers that are
multiples of both three and five print “FizzBuzz“.
22
C Workbook: Looping Statements and Arrays
1. Complete the Partial Code
What is the purpose of looping
stmt?
1. Complete the code to print numbers 1 to 5:
int i = 10;
while ( )
{
printf("%d ", i);
i--;
}
int x = 0;
do {
printf("Run");
} while ;
for
printf("* ");
printf("\n");
}
23
5. Complete the code to find the sum of first 10 natural numbers:
int sum = 0;
for ( )
sum += i;
printf("%d", sum);
int n = 5, fact = 1;
for (int i = 1; ; i++)
fact *= i;
11. Complete the loop to print only even numbers from 1 to 20:
24
13. Fill in logic to search an element in an array:
int arr[5] = {1,2,3,4,5}, key =3;
for (int i = 0; i < 5; i++)
if ( )
printf("Found at %d", i);
int count = 0;
for (int i = 0; i < 10; i++)
if (arr[i] > 0)
;
25
19. Complete the loop to sum all elements of 2D array:
int sum = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
sum += ;
while i < 5 {
printf("Loop");
i++;
}
do
printf("Running");
while(x < 5)
int i = 0;
do {
printf("%d ", i++);
} while (i = 5);
26
26. Fix the 2D array access:
int arr[5];
for (int i = 0; i <= 5; i++)
printf("%d ", arr[i]);
int fact = 1;
for (int i = 1; i <= 0; i++)
fact *= i;
int arr[5];
arr[5] = 10;
while (1)
printf("Loop");
int sum;
sum += 10;
27
printf("%d", i);
int arr[5];
int size = sizeof(arr);
printf("%d", size);
37. Predict:
Eg: for(; ; )
int i = 1;
do {
printf("Loop");
i++;
} while (i < 2);
39. Output?
40. Predict:
int sum = 0;
for (int i = 1; i <= 3; i++)
sum += i;
28
printf("%d", sum);
41. Output?
Draw a simple memory representation of 2D array.
int arr[2][2] = {{1,2},{3,4}};
printf("%d", arr[1][1]);
int i = 0;
for (; i < 3; ) {
printf("%d", i);
i++;
}
44. Predict:
int i = 1;
while (i < 4) {
printf("Loop");
i++;
}
45. Output?
46. Predict:
int a = 10;
for (; a < 13; a++)
29
printf("%d ", a);
48. Output?
What happen if we place semicolon at the end of loop
int x = 0; Like,
do {
printf("Hi "); While(cond);
x++; {
} while (x < 2);
///Stmts;
49. Predict:
}
int i;
for (i = 3; i > 0; i--)
printf("%d ", i);
50. Output?
30
Functions
#include <stdio.h> int main()
int sum(int x, int y) {
{ int a = 3, b = 2;
int c; int c = sum(a, b);
c = x + y; printf("Sum of %d and %d : %d", a, b, c);
return c; return 0;
} }
From the above code identify the following terminologies and fill the below box:
Calling function
Called function
function prototype
arguments(actual arguments)
parameters(formal
arguments)
function definition
return type
31
Solve it!
33
HackerRank Problem list
https://www.hackerrank.com/challenges/hello-world-
Hello World
c/problem?isFullScreen=true
https://www.hackerrank.com/challenges/sum-numbers-c?isFullScreen=true
Sum Numbers
Function in C https://www.hackerrank.com/challenges/functions-in-c?isFullScreen=true
Conditional https://www.hackerrank.com/challenges/conditional-statements-in-
Statements c?isFullScreen=true
Loops in C https://www.hackerrank.com/challenges/for-loop-in-c?isFullScreen=true
https://www.hackerrank.com/challenges/sum-of-digits-of-a-five-digit-
Sum of Digits
number?isFullScreen=true
Bitwise https://www.hackerrank.com/challenges/bitwise-operators-in-
operators c?isFullScreen=true
34