You are on page 1of 10

PART A

A.
1 #include <stdio.h>
int main()
{
int N = 11011;
int a = 1;
int ans = 0;
while (N != 0) {
ans = ans + (N % 10) * a;
N = N / 10;
a = a * 2;
}
printf("%d", ans);
return 0;
}
Ans:- Output is (C) 27.

2 #include<stdio.h>

int main()

int x;

x=10,20,30;

printf("%d",x);

return 0;

Ans:- Output is (C) 30

3 #include<stdio.h>
int main()

int a = 320;

char *ptr;

ptr =( char *)&a;

printf("%d ",*ptr);

return 0;

Ans:- Output is (C) 64

4 #include<stdio.h>
int main()
{
char arr[11]="The African Queen";
printf("%s",arr);
return 0;
}

Ans:- Output is (A) The African Queen

5 #include<stdio.h>
int main()
{
int _=5;
int __=10;
int ___;
___=_+__;
printf("%i",___);
return 0;

Ans:- Output is (C) 15

6 #include<stdio.h>
int main()
{
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}

Ans:- Output is (A) 0

7 #include<stdio.h>
int main()
{
int max-val=100;
int min-val=10;
int avg-val;
avg-val = max-val + min-val / 2;
printf("%d",avg-val);
return 0;

}
Ans:- Output is (D) Compilation Error
8 #define PRINT printf("Star Wars");printf(" Psycho");
#include<stdio.h>
int main()
{
int x=1;
if(x--)
PRINT
else
printf("The Shawshank Redemption");
return 0;
}

Ans:-Output is (A) Stars Wars Psycho

9 What is the meaning of the following declaration?


int(*ptr[5])();

Ans:- (B) ptr is an array of pointers to function.


10 #include<stdio.h>
int main()
{
int const X=0;
switch(5/4/3){
case X: printf("Clinton");
break;
case X+1: printf("Gandhi");
break;
case X+2: printf("Gates");
break;
default: printf("Brown");
}
return 0;
}

Ans:- Output is (A) Clinnton.

PART B
B.
1 #include <stdio.h>
int main() {
for (int i = 1; i <= 100; i++) {
if (i % 3 == 0 && i % 5 == 0) {
printf("FizzBuzz\n");
} else if (i % 3 == 0) {
printf("Fizz\n");
} else if (i % 5 == 0) {
printf("Buzz\n");
} else {
printf("%d\n", i);
}
}
return 0;
}
Output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

2 #include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers separated by space: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}

Output: Enter two numbers separated by space: 5 8


Before swapping: num1 = 5, num2 = 8
After swapping: num1 = 8, num2 = 5
3. #include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output: Enter the number of rows: 5
*
**
***
****
*****

4 The error in this code results in an infinite loop is the decrement operation i
—inside the loop condition. In this loop i starts at 0 and it will never become
less than 5 through decrement operation.

To fix this, it should be changed to i++ so that I increments with each iteration
and it never causes ani infinite loop. The correct code is----

#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
// loop body
}
return 0;}

5 #include <stdio.h>
int main() {
char str[] = "LUV";
int sum = 0;
int length = 0;

for (int i = 0; str[i] != '\0'; i++) {


sum += str[i];
length++;
}
double average = (double)sum / length;
printf("Average of ASCII values of characters in the string \"%s\" is: %.2f\n",
str, average);
return 0;

Output: Average of ASCII values of characters in the string "LUV" is: 82.33

PART C
C.

1 Input/Output function prototypes and macros are defined in the standard


input/output library file ‘stdio,h’.

2 `stdin`, `stdout`, and `stderr` are standard streams in C programming:

(i) stdin: `stdin` is the standard input stream. It is typically used for
reading input from the user or from a file. By default, it is associated
with the keyboard. Functions like `scanf()` and `fgets()` read input from
`stdin`.

(ii) stdout: `stdout` is the standard output stream. It is typically used for
writing output to the console or to a file. By default, it is associated
with the console screen. Functions like `printf()` and `fprintf()` write
output to `stdout`.

(iii) stderr : `stderr` is the standard error stream. It is typically used for
writing error messages or diagnostic information to the console or to a file. By
default, it is also associated with the console screen. However, it's important to
note that `stderr` is unbuffered, meaning that output written to `stderr` is
immediately displayed on the console without waiting for a newline character.
This is useful for printing error messages.

3. In C programming, there is no standard function called flushall(), but there


is a function called fflush().
fflush() Function:
The fflush() function is used to clear the output buffer of a stream.
When output is written to a stream (such as stdout or a file), it is often buffered,
meaning that the data is stored in a buffer in memory before it is actually written
to the output device. This buffering improves efficiency by reducing the number
of write operations.
Purpose of fflush():
The primary purpose of fflush() is to ensure that any buffered output is written
to the output device immediately.
It is commonly used with standard output (stdout) to ensure that any output
generated by printf() or other output functions is displayed immediately on the
console.

4. Yes, when you dynamically allocate memory in C using functions like


malloc(), calloc(), or realloc(), you should always free that memory once you no
longer need it. Failing to do so can lead to memory leaks, where the program
consumes memory that is no longer in use, leading to inefficient memory usage
and potential performance issues.
You can free dynamically allocated memory during runtime using the free()
function. The free() function deallocates the memory previously allocated by a
call to malloc(), calloc(), or realloc(). It takes a pointer to the memory block to
be freed as its argument.

5. const char *s: In this declaration, const is placed before char, indicating that
the character being pointed to by s is constant. This means that you cannot
modify the value of the character using the pointer s, but you can modify the
pointer itself to point to a different character.
char const *s: This declaration is equivalent to the first one. In C, the position of
the const keyword relative to the data type (in this case, char) does not matter.
So, char const *s is exactly the same as const char *s.

You might also like