0% found this document useful (0 votes)
8 views8 pages

C-Programing Test-2 (Moderate)

The document is a weekly test for C Programming with multiple choice questions and higher mark questions. It covers various topics including syntax, output of code snippets, and function behavior in C. The document also includes answer keys and explanations for the questions.

Uploaded by

vanavemihir
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)
8 views8 pages

C-Programing Test-2 (Moderate)

The document is a weekly test for C Programming with multiple choice questions and higher mark questions. It covers various topics including syntax, output of code snippets, and function behavior in C. The document also includes answer keys and explanations for the questions.

Uploaded by

vanavemihir
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/ 8

1

Branch : CS & IT

WEEKLY TEST – 02 (MODERATE)


Subject: C Programming

Maximum Marks: 26

Section 1: Multiple Choice Questions (MCQs) (B) *int constant constPtr;


(C) const int *constPtr;
Q1. What is the output of the following program?
(D) A and C both
#include<stdio.h>
Q3. What will be the output of the following
int main() program?

{ #include<stdio.h>

int i = 0; int main()

switch(i) { {

case 0 : i++; char *name="INDIA";

case 1 : i=i++; int x;

case 2 : ++i; char *cptr = name;

} while(*cptr != '\0')

printf("%d",i++); {

return 0; cptr++;

} }

Options: x = cptr - name;


(A) 5
printf("%d", x);
(B) 2
(C) 3 return 0;
(D) 4
}
Q2. Which is the correct syntax to declare a
Options:
pointer?
(A) 4
Options:
(B) 5
(A) int *const constPtr;
2

(C) 6 variables rather than the originals)


(D) Compilation Error (B) Passed by reference (i.e. the called function
has access to the original argument, not a local
Q4. What is the output of the following C code?
copy)
#include<stdio.h> (C) Non-pointer variables are passed by value
and pointers are passed by reference
int main(){
(D) None of these
int a=1, b=2;
Q7. What is the output of this C code?
if(--a && b--)
#include<stdio.h>
printf("%d %d", b,a);
static int x = 7;
else
void main()
printf("%d %d", a,b);
{
return 0;
x = 8;
}
{
Options:
int x = 10;
(A) 1 2
(B) 0 2 }
(C) 1 1
printf("%d", x);
(D) 0 1
}
Q5. Select the correct explanation for the
following declaration: Options:
(A) 7
int (*(*ptr)(int)) (void);
(B) 8
Options: (C) 10
(A) ptr is a pointer pointing to an integer function (D) Compilation Error
that takes an int value and returns an integer
Q8. What will be output if you compile and
which will point to a function with no argument
execute the following C code?
(B) ptr is pointer to function that takes an int
value returns a pointer to a function with no #include<stdio.h>
argument which returns an integer
int main()
(C) None of the above two points
(D) This is not a valid C statement {

Q6. In C, arguments to a function are always: int a[2][4]={3,6,9,12,15,18,21,24};


Options:
printf("%d %d %d", *(a[1]+2), *(*(a+1)+2),
(A) Passed by value (i.e. the called function is
2[1[a]]);
given the values of its arguments in temporary
3

return 0; {

} FILE *fp;

Options: fp=fopen("abc.c", 'w');


(A) 21 21 21
fputs("Hello", fp);
(B) 9 9 9
(C) 18 18 18 if(fp == NULL)
(D) Compilation Error
{
Q9. What will be output of the following C
printf("\nUnable to open file");
program?
exit();
#include<stdio.h>
}
int main()
fclose(fp);
{
return 0;
int b[4]={5,1,32,4};
}
int k,i,m;
Options:
k=++b[1];
(A) This will not create a file
i=b[1]++; (B) This will create a file and put Hello in the file
(C) It will print Unable to open the file
m=b[k++];
(D) None of the above
printf("%d, %d, %d", k,i,m);
Section 2: Higher Mark Questions (2 Marks
return 0; Each)

} Q11. (2 Marks) Which of the following options


can be put in the blank space of the printf in line
Options:
13 to print the value "1"?
(A) 3, 2, 32
(B) 2, 3, 32 #include<stdio.h>
(C) 3, 2, 4
struct out {
(D) Compilation Error
struct in
Q10. What is the output of the following C code?
{
#include<stdio.h>
char c;
#include<string.h>
int d;
#include<stdlib.h>
}s1;
int main()
}a1, *pa1;
4

int main() (C) The given program converts the small case
letters to the uppercase letters
{
(D) None of these
pa1=&a1;
Q13. (2 Marks) [Numerical Answer Type] What is
a1.s1.c='1'; the output of the following C code?

printf("%c", …… ); // line 13 #include<stdio.h>

return 0; int mystery(int n) {

} int v1 = 2;

Options: int v2 = 1;
(A) pa1->s1.c
while (n > 0) {
(B) pa1.s1.c
(C) a1->s1.c if (n % 2 == 1)
(D) All of the above
v2 = v1 * v2;
Q12. (2 Marks) What is the output of the
v1 = v1 * v1;
following C code?
n = n / 2;
#include <stdio.h>
}
int main(void) {
return v2;
int c;
}
for ( ; ; ) {
int main()
c = getchar();
{
if (c == EOF) break;
printf("%d", mystery(10));
if ((c >= 97) && (c < 123))
return 0;
c -= 32;
}
putchar(c);
Answer: __________
}
Q14. (2 Marks) [Numerical Answer Type] What is
return 0;
the output of the following C code?
}
#include<stdio.h>
Options:
int func(int p)
(A) The given program checks the ASCII is
between 97 and 123 {
(B) The given program checks the size of the file
static int r;
5

if(p <= 0) *(--p) = '\0';

return 1; p+=1;

if(p>10) printf("(%s) (%s)\n", s, p);

{ return 0;

r = p; }

return func(p - 10)+r; Options:


(A) (12345) (ABCDE)
}
(B) (ABCDE) (12345)
return func(p-5)+r; (C) Segmentation fault
(D) Compilation Error
}
Q16. (2 Marks) What is the output of the
int main()
following C code?
{
#include<stdio.h>
printf("%d", func(20));
void fun(int*, int);
return 0;
void (*ptr[1])(int*, int);
}
int main()
Answer: __________
{
Q15. (2 Marks) What is the output of the
int a = 2;
following C code?
int b = 4;
c
ptr[0] = fun;
Copy
ptr[0](&a, b);
Download
printf("%d %d ", a, b);
#include <stdio.h>
return 0;
#include <string.h>
}
int main() {
void fun(int *p, int q)
char s[90];
{
char *p = s;
int tmp = *p;
strcpy(s, "12345-");
*p = q;
strcat(p, "ABCDE");
q = tmp;
p += 6;
6

} print(max(x++,y),x,y);

Options: return 0;
(A) 2 2
}
(B) 4 2
(C) 4 4 Options:
(D) 2 4 (A) 2 2 2
(B) 3 3 2
Q17. (2 Marks) Find the output of the following C
(C) 3 2 3
program:
(D) Compilation Error
#include<stdio.h>

int main() {

char *a[] = {"GATE", "with", "Arpit", "2026"};

char **b[] = {a+2, a+1, a+3, a}, ***c;

c = b;

++c;

printf("%s", *(*(c)+1));

Options:
(A) GATE
(B) Arpit
(C) it
(D) 026

Q18. (2 Marks) What is the output of the


following C program?

#include<stdio.h>

#define prn(a) printf("%d", a)

#define print(a,b,c) prn(a), prn(b), prn(c)

#define max(a,b) (a<b)? b : a

int main()

int x=1, y=2;


7

Answer Key / Solutions

MCQ Solutions o Returns a pointer to another


function that takes void and
1. Answer: (B) 2 returns int.
• i starts at 0 and enters case 6. Answer: (A) Passed by value
0: i++ makes i=1 (no break → fall-through)
• C always passes arguments by value
• case 1: i=i++ is undefined behavior but (copies).
typically results in i=1 (post-increment)
• Pointers simulate pass-by-reference by
• case 2: ++i makes i=2 passing addresses (still by value).
• printf prints 2 (post-increment i++ prints 7. Answer: (B) 8
then increments to 3).
• static int x=7 is global.
2. Answer: (D) A and C both
• x=8 updates the global x.
• int *const constPtr: Constant pointer to
integer (pointer address fixed). • Local x=10 is scoped to the inner block
and discarded.
• const int *constPtr: Pointer to constant
integer (value fixed). 8. Answer: (A) 21 21 21

• Option B is invalid syntax. • a[1][2] = 21 (row 1, column 2).

3. Answer: (B) 5 • *(a[1]+2), *(*(a+1)+2), and 2[1[a]] are all


equivalent to a[1][2].
• name points to "INDIA" (length 5:
'I','N','D','I','A','\0'). 9. Answer: (A) 3, 2, 32

• cptr increments until \0, then cptr - • k=++b[1]: b[1] becomes 2 → k=2.
name = address difference = 5. • i=b[1]++: i=2, then b[1] becomes 3.
4. Answer: (B) 0 2 • m=b[k++]: m=b[2]=32, then k increments
• --a decrements a to 0 (false). to 3.

• Logical AND (&&) short-circuits: b-- is not 10. Answer: (B) Creates file with "Hello"
evaluated. • fopen("abc.c", 'w') creates the file if it
• else prints a=0, b=2 (unchanged). doesn’t exist.

5. Answer: (B) • fputs("Hello", fp) writes to the file.

• ptr is a pointer to a function that: • fp is non-NULL (successful open).

o Takes an int argument.


8

2-Mark Solutions • *(--p) = '\0' replaces '-' with null


terminator → s = "12345".
11. Answer: (A) pa1->s1.c
• p now points to "ABCDE".
• pa1 is a pointer to struct a1.
16. Answer: (C) 4 4
• -> dereferences the pointer: pa1-
>s1.c = (*pa1).s1.c = '1'. • fun(&a, b) swaps a and b via pointer:

12. Answer: (C) Converts lowercase to o *p = q → a = 4.


uppercase
o q (local copy) becomes 2 (not
• ASCII 97-122 = lowercase letters. reflected in main).

• c -= 32 converts to uppercase (e.g., 'a' 17. Answer: (A) Arpit


(97) → 'A' (65)).
• char *a[] = {"GATE", "with", "Arpit", "2026"};
13. Answer: 1024
o a[0] → "GATE", a[1] → "with", a[2] →
• mystery(10) computes 210210: "Arpit", a[3] → "2026".

o Loop • char **b[] = {a+2, a+1, a+3, a};


processes n=10 (binary 1010):
o b[0] → a+2 (→ a[2]), b[1] → a+1 (→ a[1
▪ For each set bit, ]), b[2] → a+3 (→ a[3]), b[3] → a (→ a[0
multiply v2 by v1 (squaring v ]).
1 each iteration).
• c = b; ++c;
o Final v2 = 1024.
o c initially points to b[0] → after ++c,
14. Answer: 61 points to b[1] (→ a+1).

• Recursion stack: • *(*(c)+1) evaluates as:

o func(20) → func(10) + 20 (r=20). o *c → a+1 → &a[1].

o func(10) → func(5) + 20. o *c + 1 → a+2 → &a[2].

o func(5) → func(0) + 20. o *(*c + 1) → a[2] → "Arpit".

o func(0) returns 1. 18. Answer: (A) 2 2 2

o Sum: 1 + 20 + 20 + 20 = 61. • max(x++, y) expands to (x++ < y) ? y : x++.

15. Answer: (A) (12345) (ABCDE) o x++ (1) < 2 → returns y=2,
then x increments to 2.
• strcpy(s, "12345-") → s = "12345-".
• print(2, 2, 2) prints "2 2 2".
• strcat(p, "ABCDE") → s = "12345-ABCDE".

• p += 6 points to 'A'.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy