Introduction to computer programming (ICTP) MCQ's with solved answers
1.What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- int y = 10000;
- int y = 34;
- printf("Hello World! %d\n",
y);
- return 0;
- }
a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value
2. Which of the following is not a valid variable name declaration?
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14
3.What will happen if the following C code is executed?
- #include <stdio.h>
- int main()
- {
- int main = 3;
- printf("%d", main);
- return 0;
- }
a)
It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping
4.What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- int ThisIsVariableName = 12;
- int ThisIsVariablename = 14;
- printf("%d",
ThisIsVariablename);
- return 0;
- }
a)
The program will print 12
b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration
5. Which of the following is
a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri}
Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
6. What is the size of
an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
7.What will be the
output of the following C code?
#include <stdio.h>
int main()
{
j = 10;
printf("%d\n", j++);
return 0 }
a) 10
b) 11
c) Compile time error
d) 0
8. Will the following C
code compile without any error?
- #include
<stdio.h>
- int main()
- {
- for (int k
= 0; k < 10; k++);
- return
0;
- }
a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error
9. What will be the output of the following C code?
- #include <stdio.h>
- int x;
- void main()
- {
- if (x)
- printf("hi");
- else
- printf("how are u");
- }
c) c) compile time error
d) error
10. What will be the correct
syntax for running two variable for loop simultaneously?
a)
for (i
= 0;
i < n; i++)
for (j
= 0;
j < n; j += 5)
b)
for (i = 0,
j = 0;
i < n, j < n; i++, j += 5)
c)
for (i
= 0;
i < n;i++){}
for (j
= 0;
j < n;j += 5){}
d) none of the
mentioned
11. What will be the output of the following C code?
- #include <stdio.h>
- void main()
- {
- int k = 0;
- for (k < 3; k++)
- printf("Hello");
- }
a)
Compile time error
b) Hello is printed thrice
c) Nothing
d) Varies
12. . What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- while ()
- printf("In while loop
");
- printf("After loop\n");
- }
a) In while loop after loop
b) After loop
c) Compile time error
d) Infinite loop
13. . What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- int i = 0;
- do {
- i++;
- printf("In while loop\n");
- } while (i < 3);
- }
a)
In while loop
In while loop
In while loop
b)
In while loop
In while loop
b)
Depends on the compiler
d) Compile time error
14. How many times is value is checked in the
following C code?
- #include <stdio.h>
- int main()
- {
- int i = 0;
- do {
- i++;
- printf("in while loop\n");
- } while (i < 3);
- }
a) 2
b) 3
c) 4
d) 1
15. What is the output of this C code?
- #include <stdio.h>
- main()
- {
- int n = 0, m = 0;
- if (n > 0)
- if (m > 0)
- printf("True");
- else
- printf("False");
- }
a) True
b) False
c) No Output will be printed
d) Run Time Error
16. The C statement “”if (a == 1 || b == 2) {}”” can be
re-written as ___________
a)
if (a == 1)
if (b == 2){}
b)
if (a == 1){}
if (b == 2){}
c)
if (a == 1){}
else if (b == 2){}
d) none of the mentioned
17. What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- if (printf("%d",
printf(")))
- printf("We are
Happy");
- else if (printf("1"))
- printf("We are
Sad");
- }
a) 0We are Happy
b) 1We are Happy
c) 1We are Sad
d) compile time error
18. How many times while loop condition is tested in
the following C code snippets, if i is initialized to 0 in both the cases?
- while (i < n)
- i++;
- ————-
- do
- i++;
- while (i <= n);
a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
19. What will be the output of the following C code?
- #include <stdio.h>
- int main()
- {
- int i = 0, j = 0;
- while (i < 5, j < 10)
- {
- i++;
- j++;
- }
- printf("%d, %d\n",
i, j);
- }
a) 5, 5
b) 5, 10
c) 10, 10
d) Syntax error
20. Which
loop is most suitable to first perform the operation and then test the
condition?
a) for loop
b) while loop
c) do-while loop
d) none of the mentioned
What will be the output of the following C code?
1. #include <stdio.h>
2. void m()
3. {
4. printf("hi");
5. }
6. void main()
7. {
8. m();
9. }
a) hi b) Run time error c) Nothing
What will be the output of the following C code?
1. #include <stdio.h>
2. int main()
3. {
4. void foo();
5. printf("1 ");
6. foo();
7. }
8. void foo()
9. {
10. printf("2 ");
11. }
a) 1 2 b) Compile time error c) 1 2 1 2 d) Depends on the compiler
What will be the output of the following C code?
1. #include <stdio.h>
2. void foo();
3. int main()
4. {
5. void foo(int);
6. foo();
7. return 0;
8. }
9. void foo()
10. {
11. printf("2 ");
12. }
a) 2 b) Compile time error c) Depends on the compiler d) Depends on the standard
What is the default return type if it is not specified in function definition?
a) void b) int c) double d) short int
What will be the output of the following C code?
1. #include <stdio.h>
2. double foo();
3. int main()
4. {
5. foo();
6. return 0;
7. }
8. foo()
9. {
10. printf("2 ");
11. return 2;
12. }
a) 2 b) Compile time error c) Depends on the compiler d) Depends on the standard
Can variable i be accessed by functions in another source file?
1. #include <stdio.h>
2. int i;
3. int main()
4. {
5. printf("%d\n", i);
6. }
a) Yes b) No c) Only if static keyword is used d) Depends on the type of the variable
What will be the output of the following C code?
1. #include <stdio.h>
2. static int x = 5;
3. void main()
4. {
5. x = 9;
6. {
7. int x = 4;
8. }
9. printf("%d", x);
10. }
a) 9 b) 4 c) 5 d) 0
What will be the output of the following C code? (Assuming size of int be 4)
1. #include <stdio.h>
2. struct temp
3. {
4. int a;
5. int b;
6. int c;
7. } p[] = {0};
8. main()
9. {
10. printf("%d", sizeof(p));
11. }
a) 4 b) 12 c) 16 d) Can’t be estimated due to ambiguous initialization of array
Comment on the output of the following C code.
1. #include <stdio.h>
2. struct temp
3. {
4. int a;
5. int b;
6. int c;
7. };
8. main()
9. {
10. struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
11. }
a) No Compile time error, generates an array of structure of size 3
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
Which of the following function with ellipsis are illegal?
a) void func(…);
b) void func(int, …);
c) void func(int, int, …);
d) none of the mentioned
Which header file includes a function for variable number of arguments?
a) stdlib.h
b) stdarg.h
c) ctype.h
d) both stdlib.h and stdarg.h
In a variable length argument function, the declaration “…” can _______
a) Appear anywhere in the function declaration
b) Only appear at the end of an argument list
c) Nothing
d) None of the mentioned
Which function will you choose to join two words?
a) strcpy() b) strcat() c) strncon() d) memcon()
What will be the output of the following C code?
char str1[] = "Helloworld ";
char str2[] = "Hello";
len = strspn(str1, str2);
printf("Length of initial segment matching %d\n", len );
a) 6 b) 5 c) 4 d) no match
What will be the output of the following C code?
char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat= strncmp(str1, str2, 4);
if(mat< 0)
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1");
else
printf("both are equal");
a) str1 is not greater than str2 b) str2 is not greater than str1 c) both are equal d) error in given code
What will be the output of the following C code?
char str[] =”Good”;
scanf(“%s”, str);
a) compile error b) run-time error c) good d) logical error
17. A fatal error will be generated if the format string is ended with a white space character. a) true b) false
What will be the output of the following C code?
1. #include <stdio.h>
2. struct point
3. {
4. int x;
5. int y;
6. };
7. int main()
8. {
9. struct point p = {1};
10. struct point p1 = {1};
11. if(p == p1)
12. printf("equal\n");
13. else
14. printf("not equal\n");
15. }
a) Compile time error b) equal c) depends on the standard d) not equal
Is there any function declared as strstr()?
a) true b) false
Which of the following return-type cannot be used for a function in C?
a) char * b) struct c) void d) none of the mentioned
What will be the output of the following C code?
1.
#include <stdio.h>
2.
void foo(int*);
3.
int main()
4.
{
5.
int i =
10;
6.
foo((&i)++);
7.
}
8.
void foo(int *p)
9.
{
10. printf("%d\n", *p);
11. }
) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
1.
#include <stdio.h>
2.
void foo(int*);
3.
int main()
4.
{
5.
int i =
10,
*p = &i;
6.
foo(p++);
7.
}
8.
void foo(int *p)
9.
{
10. printf("%d\n", *p);
11. }
a)10
b) Some garbage value
c) Compile time error
d) Segmentation fault
What will be the output of the following C code?
1.
#include <stdio.h>
2.
int main()
3.
{
4.
int i =
97,
*p = &i;
5.
foo(&i);
6.
printf("%d
", *p);
7.
}
8.
void foo(int *p)
9.
{
10. int
j = 2;
11. p =
&j;
12. printf("%d
", *p)
a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
What will be the
output of the following C code?
1.
#include <stdio.h>
2.
int main()
3.
{
4.
int i =
10;
5.
int *p
= &i;
6.
foo(&p);
7.
printf("%d
", *p);
8.
printf("%d
", *p);
9.
}
10. void
foo(int
**const
p)
11. {
12. int
j = 11;
13. *p
= &j;
14. printf("%d
", **p);
15. }
a) 11 11 11
b) 11 11 Undefined-value
c) Compile time error
d) Segmentation fault/code-crash
Which of the
following is the correct syntax to send an array as a parameter to function?
a) func(&array);
b) func(#array);
c) func(*array);
d) func(array[size]);
What error occurs when
a result is undefined for a given argument value?
a) significance loss
b) underflow
c) overflow
d) domain
.What will be the output of the following
C code if the input entered as first and second number is 5 and 6 respectively?
#include<stdio.h>
#include<stdlib.h>
main()
{
int *p;
p=(int*)calloc(3*sizeof(int));
printf("Enter first number\n");
scanf("%d",p);
printf("Enter second number\n");
scanf("%d",p+2);
printf("%d%d",*p,*(p+2));
free(p);
}
a) 56
b) Address of the locations where the two
numbers are stored
c) 57
d) Error
What will be the output of the following C
code?
1. #include <stdio.h>
2. int main()
3. {
4. int ary[4] = {1, 2, 3, 4};
5. printf("%d\n", *ary);
6. }
a) 1
b) Compile time error
c) Some garbage value
d) Undefined variable
What are the different ways to initialize an array with
all elements as zero?
a) int array[5] = {};
b) int array[5] = {0};
c)
int a = 0, b = 0,
c = 0;
int array[5] = {a,
b, c};
d) All of the mentioned
Which of the following
declaration is illegal?
a)
int a = 0, b = 1, c = 2;
int array[3] = {a, b, c};
b)
int size = 3;
int array[size];
c)
int size = 3;
int array[size] = {1, 2, 3};
d) All of the
mentioned
Post a Comment