C MCQ
C MCQ
C MCQ
Answer: d
Answer: d
Answer: b
Answer: c
Answer: d
Answer: d
#include <stdio.h>
int main()
{
char c = '\0';
putchar(c);
return 0;
}
Answer: b
Answer: b
4. What will be the output of the following C code if following commands are used to run
(considering myfile exists)?
1. gcc -otest test.c
2. ./test < myfile
3.
4. #include <stdio.h>
5. int main()
6. {
7. char c = 'd';
8. putchar(c);
9. }
a) Compile time error (after first command)
b) d in the myfile file
c) d on the screen
Answer: c
5. What will be the output of the following C code if following commands are used to run
(considering myfile exists)?
1. gcc -otest test.c
2. ./test > myfile
3.
4. #include <stdio.h>
5. int main(int argc, char **argv)
6. {
7. char c = 'd';
8. putchar(c);
9. printf(" %d\n", argc);
10. }
a) d 2 in myfile
b) d 1 in myfile
c) d in myfile and 1 in screen
d) d in myfile and 2 in screen
Answer: b
6. What will be the output of the following C code if following commands are used to run and
if myfile does not exist?
Answer: b
Answer: a
#include <stdio.h>
int main()
{
int i = 10, j = 2;
printf("%d\n", printf("%d %d ", i, j));
return 0;
}
#include <stdio.h>
int main()
{
int i = 10, j = 3;
printf("%d %d %d", i, j);
return 0;
}
Answer: c
#include <stdio.h>
int main()
{
int i = 10, j = 3, k = 3;
printf("%d %d ", i, j, k);
return 0;
}
Answer: c
a) myworld
b) myworld (note: 2 spaces to the left of myworld)
c) myworld (note: followed by two spaces after myworld)
d) Undefined
Answer: b
#include <stdio.h>
int main()
{
char *s = "myworld";
int i = 3;
printf("%10.*s", i, s);
return 0;
}
Answer: a
Answer: b
Answer: d
Answer: b
Answer: d
Answer: b
#include <stdio.h>
int main()
{
int n;
scanf("%d", n);
printf("%d", n);
return 0;
}
a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
Answer: b
a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
Answer: b
#include <stdio.h>
int main()
{
char n[] = "hellonworld!";
char s[13];
scanf(n, "%s", s);
printf("%s\n", s);
return 0;
}
a) hellonworld!
b)
hello
#include <stdio.h>
int main()
{
short int i;
scanf("%hd", &i);
printf("%hd", i);
return 0;
}
a) Compilation error
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
Answer: c
a) Compilation error
b) Some garbage value
c) Whatever user types
d) Depends on the standard
Answer: b
a) Compilation error
b) Undefined behavior
c) Some garbage value
d) Depends on the standard.
1. Which of the following doesn’t require an & for the input in scanf()?
a) char name[10];
b) int name[10];
c) float name[10];
d) all of the mentioned
Answer: a
8. What will be the output of the following C code (when 4 and 5 are entered)?
#include <stdio.h>
int main()
{
int m, n;
printf("Enter the numbers : ");
scanf("%d", &n);
scanf("%d", &m);
printf("%d %d", n, m);
return 0;
}
a) Error
b) 4 junkvalue
c) Junkvalue 5
d) 4 5
Answer: d
#include <stdio.h>
struct student
{
int no;
char name[20];
}
int main()
{
struct student s;
s.no = 8;
printf("hello");
return 0;
}
#include <stdio.h>
struct student
{
int no = 5;
char name[20];
};
int main()
{
struct student s;
s.no = 8;
printf("hello");
return 0;
}
a) Nothing
b) Compile time error
c) hello
d) Varies
Answer: b
#include <stdio.h>
struct student
{
int no;
char name[20];
};
int main()
{
student s;
s.no = 8;
printf("hello");
return 0;
}
a) Nothing
b) hello
c) Compile time error
d) Varies
Answer: c
#include <stdio.h>
int main()
{
struct student
{
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
return 0;
}
a) Nothing
b) Compile time error
c) Junk
d) 8
Answer: d
Answer: c
#include <stdio.h>
int main()
{
struct student
{
int no;
char name[20];
};
struct student s;
no = 8;
printf("%d", no);
return 0;
}
a) Nothing
b) Compile time error
c) Junk
d) 8
Answer: b
Answer: a
#include <stdio.h>
struct
{
int k;
char c;
};
int main()
{
struct p;
p.k = 10;
printf("%d", p.k);
return 0;
}
#include <stdio.h>
struct
{
int k;
char c;
} p;
int p = 10;
int main()
{
p.k = 10;
printf("%d %d", p.k, p);
return 0;
}
Answer: a
#include <stdio.h>
struct p
{
int k;
char c;
};
int p = 10;
int main()
{
struct p x;
x.k = 10;
printf("%d %d", x.k, p);
return 0;
}
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int p = 10;
int main()
{
struct p x = { 1, 97 };
printf("%f %d", x.f, p);
return 0;
}
7. What will be the output of the following C code according to C99 standard?
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = { .c = 97, .f = 3, .k = 1 };
printf("%f", x.f);
return 0;
}
a) 3.000000
b) Compile time error
c) Undefined behaviour
d) 1.000000
8. What will be the output of the following C code according to C99 standard?
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = { .c = 97, .k = 1, 3 };
printf("%f", x.f);
return 0;
}
a) 3.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
Answer: b
9. What will be the output of the following C code according to C99 standard?
#include <stdio.h>
struct p
{
int k;
char c;
float f;
};
int main()
{
struct p x = { .c = 97 };
printf("%f", x.f);
return 0;
}
a) 0.000000
b) Somegarbagevalue
c) Compile time error
d) None of the mentioned
1. The correct syntax to access the member of the ith structure in the array of structures is?
Assuming:
struct temp
{
int b;
}s[50];
a) s.b.[i];
b) s.[i].b;
c) s.b[i];
d) s[i].b;
Answer: d
#include <stdio.h>
struct temp
{
int a;
int b;
int c;
};
int main()
{
struct temp p[] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
return 0;
}
Answer: d
4. What is the correct syntax to declare a function foo() which receives an array of structure in
function?
a) void foo(struct *var);
b) void foo(struct *var[]);
c) void foo(struct var);
Answer: a
What will be the output of the following C code? (Assuming size of int be 4)
#include <stdio.h>
struct temp
{
int a;
int b;
int c;
} p[] = {0};
int main()
{
printf("%d", sizeof(p));
return 0;
}
a) 4
b) 12
c) 16
d) Can’t be estimated due to ambiguous initialization of array
Answer: b
struct student
{
char *name;
};
struct student s[2];
int main()
{
s[0].name = "alan";
s[1] = s[0];
printf("%s %s\n", s[0].name, s[1].name);
s[1].name = "turing";
printf("%s %s", s[0].name, s[1].name);
return 0;
}
a) alan alan
alan turing
b) alan alan
c) alan turing
alan turing
d) run time error
Answer: a
#include <stdio.h>
struct student
{
char *name;
};
struct student s[2], r[2];
int main()
{
s[0].name = "alan";
s[1] = s[0];
r = s;
printf("%s %s", r[0].name, r[1].name);
return 0;
}
a) alan alan
b) Compile time error
c) Varies
d) Nothing
Answer: b
#include <stdio.h>
struct student
{
char *name;
};
int main()
{
struct student s[2], r[2];
s[1] = s[0] = "alan";
printf("%s%s", s[0].name, s[1].name);
return 0;
}
a) alan alan
b) Nothing
c) Compile time error
b)
#include <stdio.h>
struct student
{
};
int main() {
struct student s[2];
printf("%d", sizeof(s));
return 0;
}
a) 2
b) 4
c) 8
d) 0
Answer: d
Answer: b
Answer: a
Answer: b
Answer: a
Answer: c
Answer: a
Answer: b
Answer: d
Answer: a
Answer: b
3. What will fopen will return, if there is any error while opening a file?
a) Nothing
b) EOF
c) NULL
d) Depends on compiler
Answer: c
Answer: b
5. When a C program is started, O.S environment is responsible for opening file and providing
pointer for that file?
a) Standard input
b) Standard output
c) Standard error
d) All of the mentioned
Answer: d
Answer: c
Answer: b
Answer: c
Answer: d
Answer: d
Answer: a
Answer: d
Answer: c
Answer: b
Answer: a
7. Which of the following statements about stdout and stderr are true?
a) Same
b) Both connected to screen always
c) Both connected to screen by default
d) stdout is line buffered but stderr is unbuffered
Answer: c
Answer: b
Answer: a
Answer: a
Answer: a
Explanation: fopen() opens the named file, and returns a stream, or NULL of the attempt fails.
Answer: b
Explanation: w+ is a mode used to open a text file for update (i. e., writing and reading),
discard previous contents if any.
3. If the mode includes b after the initial letter, what does it indicates?
a) text file
b) big text file
c) binary file
d) blueprint text
Answer: c
Explanation: If the mode consists of letter b after the first letter as in, “rb” or “w+b”, it
indicates binary file.
Answer: b
Explanation: fflush(FILE *stream) – fflush() causes any buffered but unwritten to be written
on an Output stream. On an input stream, the effect is undefined. fflush(NULL) flushes all
output streams.
Answer: a
Explanation: remove(const *filename) removes the named file, so that a subsequent attempt
to open it will fail. It returns non-zero of the attempt fails.
Answer: a
Explanation: A temporary file is created by tmpfile() function of mode “wb+” that will be
automatically removed when closed or when the program terminates normally.
7. What does tmpfile() returns when it could not create the file?
a) stream and NULL
b) only stream
c) only NULL
d) does not return anything
Answer: a
Explanation: tmpfile() returns a stream or NULL if it could not create the file.
Answer: b
Explanation: The fscanf() is similar to the scanf() function, except that the first argument of
fscanf() specifies a stream from which to read whereas scanf() can read from standard input.
10. fwrite() can be used only with files that are opened in binary mode.
a) true
b) false
Answer: a
Answer: d
Explanation: The fputs() is used to write a line to a file. fputs() syntax can be written as
int fputs(const char *str, FILE *stream);
Answer: a
Explanation: gets() reads the next input line into the array s, terminating newline is replaced
with ‘\0’.It returns s, or NULL if end of file or error occurs.
3. Which function will return the current file position for stream?
a) fgetpos()
b) fseek()
c) ftell()
d) fsetpos()
Answer: c
Explanation: The current file position is returned by ftell() function for stream, or -1L on
error.
Answer: a
Explanation:fgetpos() records the current position in stream in *s, for subsequent use by
fsetpos() . The type fpost_t is suitable for recording such values.
Answer: c
Explanation: ferror() is declared under <errno. h>. ferror() returns non-zero if the error
indicator for stream is set.
Answer: a
Explanation: setvbuf() and setbuf() controls buffering for the stream. If buff is NULL, buffering
is turned off for the stream.
7. The functions vprintf(), vfprintf(), and vsprintf() are not equivalent to the corresponding
printf() functions except the variable argument list.
a) true
b) false
Answer: b
Explanation: The functions vprintf() , vfprintf() , and vsprintf() are similar to the
corresponding printf() functions except that the variable argument list is replaced by arg.
8. The______function reads atmost one less than the number of characters specified by size
from the given stream and it is stored in the string str.
a) fget()
b) fgets()
c) fput()
d) fputs()
Answer: b
Explanation: The fgets() function reads one less than the number of characters indicated by
the size from the given stream and it is stored in the string str. The fgets() terminates as soon
as it encounters either a newline character, EOF, or other error.
Explanation: ungetc() pushes c back onto stream, where it will be returned on the next read.
Only one character of pushback per stream is Guaranteed.
Answer: c
Explanation: getc() is equivalent to fgetc() except that if it is a macro, it may evaluate more
than once.