MCQ Computer Science

C Programming Questions and Answers – Switch Statements – 2

C Programming Questions and Answers – Switch Statements – 2

1. What is the output of this C code?

#include <stdio.h>
const int a = 1, b = 2;
int main()
{
int x = 1;
switch (x)
{
case a:
printf(“yes “);
case b:
printf(“no\n”);
break;
}
}
a) yes no
b) yes
c) no
d) Compile time error

View Answer

Answer:d

2. What is the output of this C code?

#include <stdio.h>
#define max(a) a
int main()
{
int x = 1;
switch (x)
{
case max(2):
printf(“yes\n”);
case max(1):
printf(“no\n”);
break;
}
}
a) yes no
b) yes
c) no
d) Compile time error

View Answer

Answer:c

3. What is the output of this C code?

#include <stdio.h>
int main()
{
switch (printf(“Do”))
{
case 1:
printf(“First\n”);
break;
case 2:
printf(“Second\n”);
break;
default:
printf(“Default\n”);
break;
}
}
a) Do
b) DoFirst
c) DoSecond
d) DoDefault

View Answer

Answer:c

4. Comment on the output of this C code?

#include <stdio.h>
int main()
{
int a = 1;
switch (a)
case 1:
printf(“%d”, a);
case 2:
printf(“%d”, a);
case 3:
printf(“%d”, a);
default:
printf(“%d”, a);
}
a) No error, output is 1111
b) No error, output is 1
c) Compile time error, no break statements
d) Compile time error, case label outside switch statement

View Answer

Answer:d

5. Switch statement accepts.
a) int
b) char
c) long
d) All of the mentioned

View Answer

Answer:d

6. Comment on the output of this C code?

#include <stdio.h>
int main()
{
int a = 1;
switch (a)
{
case a:
printf(“Case A “);
default:
printf(“Default”);
}
}
a) Output: Case A
b) Output: Default
c) Output: Case A Default
d) Compile time error

View Answer

Answer:d

7. Comment on the output of this C code?

#include <stdio.h>
switch (ch)
{
case ‘a’:
case ‘A’:
printf(“true”);
}
a) if (ch == ‘a’ && ch == ‘A’) printf(“true”);
b) if (ch == ‘a’)
if (ch == ‘a’) printf(“true”);
c) if (ch == ‘a’ || ch == ‘A’) printf(“true”);
d) Both a and b

View Answer

Answer:c

C Programming Questions and Answers – For Loops – 1

1. The following code ‘for(;;)’ represents an infinite loop. It can be terminated by.
a) break
b) exit(0)
c) abort()
d) All of the mentioned

View Answer

Answer:a

2. The correct syntax for running two variable for loop simultaneously is.
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 [expand title=”View Answer”] Answer:b [/expand] 3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)

View Answer

Answer:d

4. Which of the following cannot be used as LHS of the expression in for (exp1;exp2; exp3) ?
a) Variable
b) Function
c) typedef
d) macros

View Answer

Answer:d

5. What is the output of this C code?

#include <stdio.h>
int main()
{
short i;
for (i = 1; i >= 0; i++)
printf(“%d\n”, i);

}
a) The control won’t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully terminate
d) This program will get into an infinite loop and keep printing numbers with no errors

View Answer

Answer:c

6. What is the output of this C code?

#include <stdio.h>
void main()
{
int k = 0;
for (k)
printf(“Hello”);
}
a) Compile time error
b) hello
c) Nothing
d) Varies

View Answer

Answer:a

7. What is the output of this 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

View Answer

Answer:a

8. What is the output of this C code?

#include <stdio.h>
void main()
{
double k = 0;
for (k = 0.0; k < 3.0; k++)
printf(“Hello”);
}
a) Run time error
b) Hello is printed thrice
c) Hello is printed twice
d) Hello is printed infinitely

View Answer

Answer:b

For more Visit the Link:

Computer Science MCQ All Topic C programming MCQ All Topic

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button