0% found this document useful (0 votes)
17 views34 pages

C Workbook

Uploaded by

harun8827707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views34 pages

C Workbook

Uploaded by

harun8827707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

C – Workbook

INTRODUCTION

1. Fill in the statement below about the history of C with the correct answer:

C was invented by at Bell Labs in and became popular in 1978. It


was first written to design the operating system based on an older language called
language. Later on, newer languages such as C++, Java and C# were based on C.

[BCPL, B, 1972, Dennis Ritchie, Ken Thompson, Unix, Linux, 1973]

2. Match the definition correctly:

Program A set of instructions that tells the computer what

Programming Language to do

Programmer
A computer language program used to develop
applications, scripts, or other set of instructions for a
computer to execute.

A person who writes a program

3. State three type of programming languages.


a.
b.
c.

2
4. Match the following.

Assembly • Natural language of a particular computer

• Consists of strings of numbers{ls, Os)


Machine • Instruct computer to perform elementary operations one at a
time

• Machine dependent
High level

• English like abbreviations

• Use a mnemonics, e.g LOAD, ADD..

• Translators programs called "Assemblers" to convert assembly


language programs to machine language.

• They are closer to human languages and further from machine


language.

• More "English-like", therefore easier for the programmer to "think"


in the programming language.

• The main advantage of high-level languages over low-level languages


is that they are easier to read, write, and maintain.

• Ultimately, programs written in a high-level language must be


translated into machine language by a compiler or interpreter.

5. State 3 types of programming


a.

b.

c.

3
6. Match the following:

A type of programming in which programmers define not only


Structured
the data type of a data structure, but also the types of operations
programming
(functions) that can be applied to the data structure.

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:

Algorithm is the set of well-defined in sequence to .


Three different techniques of problem solving are algorithms , and
.
Algorithms are divided into three sections; , and .
For example, algorithm to fry the egg;

is the input, is the process and is the output.

[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

Section A: Fill in the blanks (5 Questions)

1. The underscore ( _ ) character is usually used in names.

2. The character is used to terminate a C statement.

3. The character is used for multi-line comments along with `*`.

Section B: Complete the partial code (5 Questions)

4. Complete the following `printf()` statement to print: Welcome@2025!

printf( );

5. Fill in missing characters to print:

Hello

World

printf("Hello\n ");

6. Fix the syntax of the following code:

int main()

printf("Hello World") // missing something

return 0 }

5
7. Use escape characters to print: C:\new\file.txt

printf( );

8. Replace the ? in the code so that it compiles without error:

#include ?

int main()

return 0;

Section C: Predict the output (5 Questions)

9. What will be the output?

printf("100%% Complete");

Identify the header file required for


10. What is the output of the following? using printf().

printf("I love\tC language");

11. Output?

printf("Line1\nLine2");

12. Output?

printf("Price: Rs.\t500");

6
13. Output?

printf("She said, \"Hello!\"");

Section D: Quiz – Multiple Choice Questions (3 Questions)

14. Which of the following is not a valid character in C?

a) @ b) # c) ^ d) ~

15. What does \n represent in C?

a) Tab b) Newline c) Null d) None

16. Which character is used for single-line comments in C99?

a) // b) /* c) ** d) --

Section E: True or False (2 Questions)

17. # is used for preprocessor directives like #include. (True / False)

18. The character \t is used to insert a tab space in output. (True / False)

List the escape sequences available in C language.

7
C Programming Workbook
Topic 2: Variables
Section A: Fill in the blanks (5 Questions)

1. A variable must be declared with a before it is used.

2. The default value of an uninitialized local variable is .

3. The keyword is used to declare constant variables.

4. A variable name can start with a or .

5. The symbol is allowed in variable names.

Section B: Complete the partial code (5 Questions)

6. Declare an integer variable named `age` and assign it the value 20.

7. Declare three float variables `x`, `y`, `z` in a single line.

8. Fix the error in the following declaration:

int 9rate = 100;

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)

11. What will be the output?

int a = 10, b = 20;

printf("%d", a + b);

12. Output? Define variable.

int x;

printf("%d", x);

// x is uninitialized

13. Output?

int a = 5;
Define Const.
a = a + 10;

printf("%d", a);

14. Output?

const int pi = 3.14;

pi = 3.14159;

printf("%f", pi);

15. Output?

int a = 3, b = 4;

printf("%d %d", b, a);

10
Section D: Quiz – Multiple Choice Questions (3 Questions)

16. Which of the following is a valid variable name?

a) 1value b) _sum c) float d) a+b

17. What is the size of an `int` variable (in most systems)?

a) 1 byte b) 2 bytes c) 4 bytes d) 8 bytes

18. What will happen if you use a variable without declaring it?

a) Compile error b) Runtime error c) Output is 0 d) Nothing happens

Section E: True or False (2 Questions)

19. Variable names in C are case-sensitive. (True / False)

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

1. The operator ` ` is used to add two values in C.

2. ` ` operator is used to find the remainder of a division.

3. The logical AND operator in C is written as ` `.

4. In C, ` ` is used to increment a value by 1.

5. The result of `5 > 3` is ` `.

6. The assignment operator is written as ` `.

7. ` ` is used for bitwise OR operation.

8. ` ` operator has the highest precedence among arithmetic operators.

9. The operator ` ` is used to compare equality.

10. The expression `a += 5;` is equivalent to `a = a 5;`

2. Complete the Partial Code

11. Complete the code to perform multiplication:

int a = 4, b = 5;

int result = ;

printf("%d", result);

12. Fill in the missing logical operator:

int a = 10, b = 20;

if (a < b b < 30) {

12
printf("Condition met");

13. Complete the compound assignment: What is ternary operator?

int x = 10;

x 3;

printf("%d", x);

14. Complete the code to check if a number is even:

int num = 8;

if (num 2 == 0) {

printf("Even");

15. Fill in the missing relational operator:

int marks = 75;

if (marks 50) {

printf("Pass");

13
3. Predict the Output

16. What will be the output of the following code?

int x = 5;

printf("%d", x++);

printf("%d", ++x);

17. Predict the output:

int a = 10, b = 5;

int c = a > b && b < 10;

printf("%d", c); What do you understand about the term operator


Precedence?

18. Predict the output:

int a = 7;

int b = a % 3;

printf("%d", b);

19. Predict the output:

int x = 4;

x *= 2 + 1;

printf("%d", x);

20. Predict the output:

int a = 1, b = 0;

int c = a || b && a; printf("%d", c);

14
4. Quiz (Multiple Choice Questions)

21. Which of the following is the correct operator for bitwise AND?

a) && b) & c) and d) &&&

22. What is the output of 10 / 3?

a) 3 b) 3.33 c) 3.0 d) Error

23. Which operator is used for logical OR in C?

a) || b) | c) or d) &&

24. What is the result of `4 + 5 * 2`?

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

26. The modulo operator returns:

a) Quotient b) Remainder c) Division d) Floor value

27. Which of the following is a unary operator?

a) + b) - c) ++ d) *

28. In the expression `a += b`, which operation is performed?

a) a = a + b b) a = b c) b = a + b d) a = a - b

29. What is the result of `5 > 3 && 2 < 1`?

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:

int num = 10;


if ( )
printf("Positive");

2. Fill in the missing part to execute an alternative block:

int num = -5;


if (num > 0)
printf("Positive");
List the types of conditional statements in c

printf("Negative");

3. Complete the else-if ladder:

int marks = 85;


if (marks >= 90)
printf("A Grade");
(marks >= 75)
printf("B Grade");
else What happens if we fail to provide break at end of
printf("C Grade"); Each case?

4. Complete the switch statement:

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");

6. Complete the switch default case:

int opt = 3;
switch(opt) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
: printf("Other");
}

7. Fill in missing logic for checking leap year:

int year = 2020;


if ((year % 4 == 0 && year % 100 != 0) (year % 400 == 0))
printf("Leap Year");

8. Complete this else-if ladder for grading:

if (score >= 90) printf("A");


(score >= 80) printf("B");
else printf("C"); Is default case mandatory to be placed at the
end of all cases?
9. Add missing condition for age check:

int age = 18;


if ( )
printf("Eligible");

10. Complete switch for traffic light:

char light = 'R';


switch(light) {
case 'R': printf("Stop"); break;
case 'G': printf("Go"); break;
: printf("Caution");
}

17
11. Complete the nested if:

int a = 4, b = 4;
if (a == b)
if ( )
printf("Equal and Even");

12. Add condition to find largest of three:

if ( && )
printf("A is largest");
if (b > c)
printf("B is largest");
else
printf("C is largest");

13. Add check for even or odd:

int num = 7;
if ( )
printf("Even");
else
printf("Odd");

14. Fill in switch case break:

switch(x) {
case 1: printf("One"); ;
default: printf("Default");
}

15. Add condition for checking two-digit number:

if ( __________________________________________)
printf("Two-digit number");

16. Fix the logic to check if a number is negative:

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;
}

3. Predict the Output

1. What is the output? 2. Predict:

int a = 5; int a = -3;


if (a > 0) if (a > 0)
printf("Positive"); printf("Positive");
else else
printf("Negative"); printf("Negative");

3. Output? 4. Result of:


int x = 5;
int x = 10; switch(x) {
if (x % 2 == 0) case 1: printf("One"); break;
printf("Even"); case 5: printf("Five"); break;
else default: printf("Default");
printf("Odd"); }

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");

9. Predict: 10. Result:

switch(2) { int x = 1;
case 1: if (x > 0)
case 2: printf("Yes");
printf("Matched"); printf("!");
break;

11. Output of: 12. Print result:

int score = 95; int x = 3;


if (score >= 90) if (x < 0)
printf("A"); printf("Neg");
else if (score >= 80) else
printf("B"); printf("Pos");

13. What is printed? 14. Predict:

int x = 10; int x = 0;


switch(x) { if (x == 0)
default: printf("No Case"); printf("Zero");
}

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:

for (int i = 1; i <= 5; )


printf("%d ", i);

2. Complete the while loop to print numbers 10 to 1:

int i = 10;
while ( )
{
printf("%d ", i);
i--;
}

List the type of loops and its syntax.


3. Complete the do-while loop to run at least once:

int x = 0;
do {
printf("Run");
} while ;

4. Fix the nested loop to print a 3x3 matrix:

for (int i = 0; i < 3; i++) {

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);

6. Complete the array declaration and initialization:

int arr[5] = __________________________________ ;

7. Complete the loop to access array elements:

for (int i = 0; i < 5; i++)


printf("%d ", );

8. Fix the loop to calculate the factorial of a number:

int n = 5, fact = 1;
for (int i = 1; ; i++)
fact *= i;

9. Complete the loop to reverse an array:

for (int i = 4; i >= 0; i--)


printf("%d ", arr[ ]);

10. Complete the array input code:

for (int i = 0; i < 5; i++)


scanf("%d", &arr[ ]);

11. Complete the loop to print only even numbers from 1 to 20:

for (int i = 1; i <= 20; i++)


if ( )
printf("%d ", i);

12. Complete the array copy logic:

for (int i = 0; i < 5; i++)


copy[i] = ;

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);

14. Complete the initialization of a 2D array:

int matrix[2][2] = _____________________________________________________________________ ;

15. Complete the loop to display 2D array elements:

for (int i = 0; i < 2; i++) {


for (int j = 0; j < 2; j++)
printf("%d ", );
printf("\n");
}

16. Complete the loop to count positive elements in an array:

int count = 0;
for (int i = 0; i < 10; i++)
if (arr[i] > 0)
;

17. Fill in to find max element in an array:

int max = arr[0];


for (int i = 1; i < 5; i++)
if (arr[i] > max)
max = ;

18. Complete the loop to print array in reverse:


int a[6] = {1,2,3,4,5,6};
for ( ; i >= 0; i--)
printf("%d ", arr[i]);

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 += ;

2. Debug the Code

Give the differences between while and do..while:


21. Debug:

for int i = 0; i < 10; i++)


printf("%d", i);

22. Find and fix:

while i < 5 {
printf("Loop");
i++;
}

23. Fix logic:

int arr[3] = {1, 2, 3};


printf("%d", arr[3]);

24. Correct the do-while loop:

do
printf("Running");
while(x < 5)

25. Debug loop:

int i = 0;
do {
printf("%d ", i++);
} while (i = 5);

26
26. Fix the 2D array access:

int a[2][2] = {{1,2},{3,4}}; What is an Array?


printf("%d", a[2][1]);

27. Fix off-by-one error:

int arr[5];
for (int i = 0; i <= 5; i++)
printf("%d ", arr[i]);

28. Debug factorial logic:

int fact = 1;
for (int i = 1; i <= 0; i++)
fact *= i;

29. Correct index issue:

int arr[5];
arr[5] = 10;

30. Fix infinite loop:

while (1)
printf("Loop");

31. Debug 2D array initialization:

int mat[2][2] = {1,2,3};

32. Fix invalid loop variable:

for (i = 0; i < 5; i++)


printf("%d", i);

33. Correct uninitialized variable:

int sum;
sum += 10;

34. Fix logical error:

for (int i = 0; i < 10; i--)

27
printf("%d", i);

35. Debug misuse of sizeof:

int arr[5];
int size = sizeof(arr);
printf("%d", size);

3. Predict the Output


36. Output? What happens if the condition is left
Empty in while loop?
int i = 0; Eg: while()
while (i < 3) {
printf("%d ", i);
i++;
}

37. Predict:
Eg: for(; ; )
int i = 1;
do {
printf("Loop");
i++;
} while (i < 2);

38. What is printed?

int arr[3] = {1, 2, 3};


printf("%d", arr[1]);

39. Output?

for (int i = 2; i <= 10; i += 2)


printf("%d ", i);

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]);

42. Predict result:

int i = 0;
for (; i < 3; ) {
printf("%d", i);
i++;
}

43. What is printed?

int arr[] = {10, 20, 30};


printf("%d", sizeof(arr)/sizeof(arr[0]));

44. Predict:

int i = 1;
while (i < 4) {
printf("Loop");
i++;
}

45. Output?

for (int i = 0; i < 2; i++)


for (int j = 0; j < 2; j++)
printf("%d ", i + j);

46. Predict:

int arr[5] = {1};


printf("%d", arr[4]);

47. What is printed?

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?

int a[3] = {5, 10, 15};


for (int i = 0; i < 3; i++)
printf("%d ", a[i]);

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!

Across 12. identify the


4. A named location in a operator: ++
memory, used to store a data 14. where does the
value execution of any C
7. pictorial representation of program begin?
an algorithm 15. data types used to store
9. which decision making real numbers
Down
statement used for menu 16. built in function used to
selection find the square root 1. which built in function is
10. which function is used to
used to read the data from
print the output? keyboard?
11. data type used to store 2. Pre-defined words in a C
whole numbers compiler
3. Who is the father of C
language?
5. \n refers to ?
6. which operators are used
32
to compare 2 quantities?
8. data type that has no
value
13. Program which converts
the C program into machine
code

33
HackerRank Problem list

https://www.hackerrank.com/challenges/hello-world-
Hello World
c/problem?isFullScreen=true

Playing with https://www.hackerrank.com/challenges/playing-with-


Characters characters?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

You might also like