PracticeQuestions ESC101 Summer
PracticeQuestions ESC101 Summer
PracticeQuestions ESC101 Summer
weeks.
Question 2
#include <stdio.h>
int main()
{
printf(5 + "GeeksQuiz");
return 0;
}
Question 3
#include <stdio.h>
int main()
{
int sum = 2 + 4 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}
Question 4
#include <stdio.h>
int main()
{
int a = 3;
int res = a++ + ++a + a++ + ++a;
printf("%d", res);
return 0;
}
Question 5
#include <stdio.h>
int main()
{
int i = 2, j=2;
{
int i = 4, j = 5;
printf("%d %d\n", i, j);
}
printf("%d %d", i, j);
return 0;
}
Question 6
What is the output of this statement "printf("%d", (a++))"?
a. The value of (a + 1)
b. The current value of a
c. Error message
d. Garbage
Question 7
#include <stdio.h>
int main()
{
int num = 58;
num = num % 11;
printf("%d", num);
return 0;
}
Question 8
int main()
{
int a=10, b=20;
printf("a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d b=%d",a,b);
return 0;
}
Question 9
#include<stdio.h>
int main()
{
int a = 12;
switch(a)
{
case 15:
printf("Suzuki\n");
break;
case 10:
printf("APACHE\n");
break;
default:
printf("OlaElectric\n");
break;
}
printf("HEROHONDA");
return 0;
}
Question 10
int main()
{
char code='K';
switch(code)
{
case 'A': printf("ANT ");break;
case 'K': printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
}
Question 11
#include <stdio.h>
int main()
{
char code='A';
switch(code)
{
case 64+1: printf("ANT ");break;
case 8*8+4: printf("KING "); break;
default: printf("NOKING");
}
printf("PALACE");
return 0;
}
Question 12
int main()
{
int k=64;
switch(k)
{
case k<64: printf("SHIP ");continue;
case k>64: printf("BOAT "); continue;
default: printf("PETROL");
}
printf("CHILLY");
}
Question 13
#include <stdio.h>
int main()
{
int s1 ,s3, num1, num2;
float s2;
num1 = scanf("%d %f %c", &s1, &s2, &s3);
num2 = printf("%d\n", num1);
printf("%d %d", num1, num2);
return 0;
}
Question 14
#include <stdio.h>
int main()
{
int a = 0, b = 5, c = 3;
b != !a;
c = !!a;
printf("%d\t%d", b, c);
}
Question 15
#include <stdio.h>
int main()
{
float x = -11.99;
printf("The number is %d",(int)x);
return 0;
}
Solutions (Tutorial 1)
int num= n; //To store the original number in the variable num
return 0;
}
Question 3
Fill in the blank to make the code run properly. (FIBONACCI SERIES)
#include <stdio.h>
int main() {
int i, n;
return 0;
}
Question 4
Fill in the blank to make the code run properly. (PRIME NUMBERS)
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
Question 5
Fill in the blank to make the code run properly. (ARMSTRONG NUMBERS)
#include <stdio.h>
int main() {
int num, originalNum, rem, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
rem = originalNum % 10;
result += ______________________;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Question 6
Fill in the blank to make the code run properly. (FACTORIAL OF A NUMBER)
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact = ______;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Question 7
What will be the output of this C code?
#include<stdio.h>
int main()
int a = 0, i = 0, b;
a++;
if (i == 3)
break;
printf("%d",a);
return 0;
Question 8
What will be the output of this C code?
#include<stdio.h>
int main()
{
int a = 0, i = 0, b;
for (i = 0; i < 5; i++)
{
a++;
if (i == 3)
continue;
}
printf("%d",a);
return 0;
}
Question 9
What will be the output of this C code?
#include <stdio.h>
void m()
{
static int x = 5;
x++;
printf("%d\n", x);
}
void main()
{
m();
m();
}
Question 10
What will be the output of this C code?
#include <stdio.h>
#include <math.h>
int main()
{
printf("%f\n",sqrt(10.0));
printf("%f\n",exp(4.0));
printf("%f\n",log(4.0));
printf("%f\n",log10(100.0));
printf("%f\n",fabs(-5.2));
printf("%f\n",ceil(4.5));
printf("%f\n",floor(-4.5));
printf("%f\n",pow(4.0,.5));
printf("%f\n",fmod(4.5,2.0));
printf("%f\n",sin(0.0));
printf("%f\n",cos(0.0));
printf("%f\n",tan(0.0));
return 0;
}
(Kindly run this code to understand better)
Question 11
Complete the code to print the following pattern using while loop:
1
12
123
1234
12345
#include <stdio.h>
int main() {
int end = 5;
int i = 1;
while (i <= end)
{
int j = 1;
while (_____)
{
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;}
Question 12
Complete the code to print the following pattern using for loop:
1
12
123
1234
12345
#include <stdio.h>
int main() {
int n = 5;
// Outer loop for rows
for (int i = 1; i <= ____; i++) {
// Inner loop for columns
for (int j = 1; j <= ____; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Question 13
What will be the output of the C program?
#include <stdio.h>
int x=4;
void m()
{
printf("%d\n",x);
}
void main()
{
m();
x = x + 1;
m();
}
Question 14
What will be the output of the C program?
#include <stdio.h>
int x = 5;
void main()
{
int x = 3;
printf("%d\n", x);
{
int x = 4;
}
printf("%d", x);
}
Question 15
What will be the output of the C program?
#include <stdio.h>
int main()
{
int x = -1;
if(x<2)
{
int x = 1;
printf("%d\n", x);
}
int x = 15;
printf("%d", x);
return 0;
}
Solutions (Tutorial 2)
Question 2
Is the below logic correct to implement bubble sort(True or False)?
Question 3
What will be the output of the C program?
#include<stdio.h>
void main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
}
Question 4
What will be the output of the C program?
The ______ function appends not more than n characters.
a) strcat()
b) strcon()
c) strncat()
d) memcat()
Question 5
What will be the output of the C program?
If the two strings are identical, then strcmp() function returns
A) -1
B) 1
C) 0
D) Yes
Question 6
What is this program meant to do?
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{
length++;
s++;
}
return (length);
}
Question 7
What will be the output of the following program?
#include<stdio.h>
#define square(x) x*x
void main()
{
int i;
i = 64/square(4);
printf("%d", i);
}
Question 8
What will be the output of the following program?
#include<stdio.h>
#define a 10
void main()
{
#define a 50
printf("%d", a);
}
A. 50
B. 10
C. Compiler Error
D. None of These
Question 9
What will be the output of the following program?
#include<stdio.h>
#define prod(a,b) a*b
void main()
{
int x=3,y=4;
printf("%d", prod(x+2,y-1));
}
Question 10
What will be the output of the following program?
#include<stdio.h>
#define max 5
void main(){
int i = 0;
i = max++;
printf("%d", i++);
}
Question 11
What will be the output of the following program?
#include<stdio.h>
int myshow(int *);
void main()
{
int a=10;
myshow(&a);
}
Question 12
What will be the output of the following program?
#include<stdio.h>
void myshow(int *);
void main()
{
int a=10;
printf("%d ", a);
myshow(&a);
printf("%d", a);
Question 13
What will be the output of the following program?
#include<stdio.h>
void myshow(int);
void main()
{
int a=10;
printf("%d ", a);
myshow(a);
printf("%d", a);
}
void myshow(int a)
{
a=20;
}
Question 14
What will be the output of the following program?
#include <stdio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}
int main() {
int ageArray[] = {2, 8, 4, 12};
display(ageArray[1], ageArray[2]);
return 0;
}
Question 15
What will be the output of the following program?
#include <stdio.h>
#include <stdlib.h>
void fun(int arr[]) // SAME AS void fun(int *arr)
{
unsigned int n = sizeof(arr)/sizeof(arr[0]);
printf("\nArray size inside fun() is %d", n);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Array size inside main() is %d", n);
fun(arr);
return 0;
}
Solutions (Tutorial 3)
Ques Answer Explanation
1 10 1) 8, 7, 9, 22, 5, 13, 31 = 4 swaps
2) 7, 8, 9, 5, 13, 22, 31 = 3 swaps
3) 7, 8, 5, 9, 13, 22, 31 = 1 swap
4) 7, 5, 8, 9, 13, 22, 31 = 1 swap
5) 5, 7, 8, 9, 13, 22, 31 = 1 swap
Total 10 swaps are required to sort the array.
NOTE: The number of passes and the number of swaps
required are two different things.
2 False for (i = 0; i < size - 1; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}
The first for loop should have ‘size-1' as the condition.
3 3, 2, 15 Just follow normal logic
4 (c) strncat() The strncat() function appends not more than n characters
from the array(s2) to the end of the string(s1)
5 (c) 0 The strcmp return an int value that is:
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
6 Find length of the string ‘s’ The function calculates length of the string ‘s’
7 64 64/4*4 = 64
8 (A) 50 The preprocessor directives can be redefined anywhere in
the program. So the most recently assigned value will be
taken.
9 10 x+2*y-1 = x+(2*y)-1 = 10
10 Compile time error The error in the code is the use of the ++ operator on the
macro max. Macros are treated as constants and do not
behave like variables. The ++ operator can only be applied
to variables, not constants.
11 Received 10, It is called Passing a variable by reference. You are passing
&a instead of a. Address of a or &a is received as int *k.
Observe the function prototype declaration before main(),
int myshow(int *).
12 10 20 We passed ‘&a’ instead of a into myshow(int) function.
*k=20 changes the valued of passed variable passed by
reference.
13 10 10 We are passing only value of ‘a’, and hence new memory
in allocated for the ‘a’ present inside the function
myshow().
14 8 Logical, we are not passing the whole array, but just it’s
4 two elements
15 Array size inside main() is 8 In C, when we pass an array to a function say fun(), it is
Array size inside fun() is 2 always treated as a pointer by fun(). The below example
demonstrates the same.
// Note that arr[] for fun is just a pointer even if square
brackets are used.