objective programs

27
http://www.cprogramming.com/tutorial/c/quiz/answer3.html 1. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run? A. 10 B. 9 C. 0 D. 1 Note: This quiz question probably generates more email to the webmaster than any other single item on the site. Yes, the answer really is 10. If you don't understand why, think about it this way: what condition has to be true for the loop to stop running? 2. When does the code block following while(x<100) execute? A. When x is less than one hundred B. When x is greater than one hundred C. When x is equal to one hundred D. While it wishes 3. Which is not a loop structure? A. For B. Do while C. While D. Repeat Until 4. How many times is a do while loop guaranteed to loop? A. 0 B. Infinitely C. 1 D. Variable

Upload: mcomputer

Post on 29-Oct-2014

112 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Objective Programs

http://www.cprogramming.com/tutorial/c/quiz/answer3.html

1. What is the final value of x when the code int x; for(x=0; x<10; x++) {} is run?A. 10B. 9C. 0D. 1

Note: This quiz question probably generates more email to the webmaster than any other single item on the site. Yes, the answer really is 10. If you don't understand why, think about it this way: what condition has to be true for the loop to stop running?

2. When does the code block following while(x<100) execute?A. When x is less than one hundredB. When x is greater than one hundredC. When x is equal to one hundredD. While it wishes

3. Which is not a loop structure?A. ForB. Do whileC. WhileD. Repeat Until

4. How many times is a do while loop guaranteed to loop?A. 0B. InfinitelyC. 1D. Variable

Page 2: Objective Programs

http://www.indiabix.com/c-programming/control-instructions/008001

1. How many times "IndiaBIX" is get printed?

#include<stdio.h>int main(){ int x; for(x=-1; x<=10; x++) { if(x < 5) continue; else break; printf("IndiaBIX"); } return 0;}

A. Infinite times B. 11 times

C. 0 times D. 10 times

View Answer C Compiler Report Discuss in Forum

2. How many times the while loop will get executed if a short int is 2 byte wide?

#include<stdio.h>int main(){ int j=1; while(j <= 255) { printf("%c %d\n", j, j); j++; } return 0;}

A. Infinite times B. 255 times

C. 256 times D. 254 times

View Answer C Compiler Report Discuss in Forum

3. Which of the following is not logical operator?

A. & B. &&

C. || D. !

View Answer C Compiler Report Discuss in Forum

4. In mathematics and computer programming, which is the correct order of mathematical operators ?

Page 3: Objective Programs

A. Addition, Subtraction, Multiplication, Division

B. Division, Multiplication, Addition, Subtraction

C. Multiplication, Addition, Division, Subtraction

D. Addition, Division, Modulus, Subtraction

View Answer C Compiler Report Discuss in Forum

5. Which of the following cannot be checked in a switch-case statement?

A. Character B. Integer

C. Float D. enum

View Answer C Compiler Report Discuss in Forum

Page 4: Objective Programs

1. What will be the output of the program?

#include<stdio.h>int main(){ int i=0; for(; i<=5; i++); printf("%d,", i); return 0;}

A. 0, 1, 2, 3, 4, 5 B. 5

C. 1, 2, 3, 4 D. 6

View Answer C Compiler Report Discuss in Forum

2. What will be the output of the program?

#include<stdio.h>int main(){ char str[]="C-program"; int a = 5; printf(a >10?"Ps\n":"%s\n", str); return 0;}

A. C-program B. Ps

C. Error D. None of above

View Answer C Compiler Report Discuss in Forum

3. What will be the output of the program?

#include<stdio.h>int main(){ int a = 500, b = 100, c; if(!a >= 400) b = 300; c = 200; printf("b = %d c = %d\n", b, c); return 0;}

A. b = 300 c = 200 B. b = 100 c = garbage

C. b = 300 c = garbage D. b = 100 c = 200

View Answer C Compiler Report Discuss in Forum

Page 5: Objective Programs

4. What will be the output of the program?

#include<stdio.h>int main(){ unsigned int i = 65535; /* Assume 2 byte integer*/ while(i++ != 0) printf("%d",++i); printf("\n"); return 0;}

A. Infinite loop

B. 0 1 2 ... 65535

C. 0 1 2 ... 32767 - 32766 -32765 -1 0

D. No output

View Answer C Compiler Report Discuss in Forum

5. What will be the output of the program?

#include<stdio.h>int main(){ int x = 3; float y = 3.0; if(x == y) printf("x and y are equal"); else printf("x and y are not equal"); return 0;}

A. x and y are equal B. x and y are not equal

C. Unpredictable D. No output

View Answer C Compiler Report Discuss in Forum

6. What will be the output of the program, if a short int is 2 bytes wide?

#include<stdio.h>int main(){ short int i = 0; for(i<=5 && i>=-1; ++i; i>0) printf("%u,", i); return 0;}

A. 1 ... 65535 B. Expression syntax error

Page 6: Objective Programs

C. No output D. 0, 1, 2, 3, 4, 5

View Answer C Compiler Report Discuss in Forum

7. What will be the output of the program?

#include<stdio.h>int main(){ char ch; if(ch = printf("")) printf("It matters\n"); else printf("It doesn't matters\n"); return 0;}

A. It matters B. It doesn't matters

C. matters D. No output

View Answer C Compiler Report Discuss in Forum

8. What will be the output of the program?

#include<stdio.h>int main(){ unsigned int i = 65536; /* Assume 2 byte integer*/ while(i != 0) printf("%d",++i); printf("\n"); return 0;}

A. Infinite loop

B. 0 1 2 ... 65535

C. 0 1 2 ... 32767 - 32766 -32765 -1 0

D. No output

View Answer C Compiler Report Discuss in Forum

9. What will be the output of the program?

#include<stdio.h>int main(){ float a = 0.7; if(0.7 > a) printf("Hi\n");

Page 7: Objective Programs

else printf("Hello\n"); return 0;}

A. Hi B. Hello

C. Hi Hello D. None of above

View Answer C Compiler Report Discuss in Forum

10. What will be the output of the program?

#include<stdio.h>int main(){ int a=0, b=1, c=3; *((a) ? &b : &a) = a ? b : c; printf("%d, %d, %d\n", a, b, c); return 0;}

A. 0, 1, 3 B. 1, 2, 3

C. 3, 1, 3 D. 1, 3, 1

View Answer C Compiler Report Discuss in Forum

11. What will be the output of the program?

#include<stdio.h>int main(){ int k, num = 30; k = (num < 10) ? 100 : 200; printf("%d\n", num); return 0;}

A. 200 B. 30

C. 100 D. 500

View Answer C Compiler Report Discuss in Forum

12. What will be the output of the program?

#include<stdio.h>int main(){ int a = 300, b, c; if(a >= 400) b = 300; c = 200;

Page 8: Objective Programs

printf("%d, %d, %d\n", a, b, c); return 0;}

A. 300, 300, 200 B. Garbage, 300, 200

C. 300, Garbage, 200 D. 300, 300, Garbage

View Answer C Compiler Report Discuss in Forum

13. What will be the output of the program?

#include<stdio.h>int main(){ int x=1, y=1; for(; y; printf("%d %d\n", x, y)) { y = x++ <= 5; } printf("\n"); return 0;}

A.

2 13 14 15 16 17 0

B.

2 13 14 15 16 1

C.

2 13 14 15 1

D.

2 23 34 45 5

View Answer C Compiler Report Discuss in Forum

14. What will be the output of the program?

#include<stdio.h>int main(){ int i = 5; while(i-- >= 0) printf("%d,", i); i = 5; printf("\n"); while(i-- >= 0) printf("%i,", i); while(i-- >= 0) printf("%d,", i); return 0;}

Page 9: Objective Programs

A.4, 3, 2, 1, 0, -14, 3, 2, 1, 0, -1

B.5, 4, 3, 2, 1, 05, 4, 3, 2, 1, 0

C. Error D.5, 4, 3, 2, 1, 05, 4, 3, 2, 1, 05, 4, 3, 2, 1, 0

View Answer C Compiler Report Discuss in Forum

15. What will be the output of the program?

#include<stdio.h>int main(){ int i=3; switch(i) { case 1: printf("Hello\n"); case 2: printf("Hi\n"); case 3: continue; default: printf("Bye\n"); } return 0;}

A. Error: Misplaced continue B. Bye

C. No output D. Hello Hi

View Answer C Compiler Report Discuss in Forum

16. What will be the output of the program?

#include<stdio.h>int main(){ int x = 10, y = 20; if(!(!x) && x) printf("x = %d\n", x); else printf("y = %d\n", y); return 0;}

A. y =20 B. x = 0

C. x = 10 D. x = 1

View Answer C Compiler Report Discuss in Forum

17. What will be the output of the program?

Page 10: Objective Programs

#include<stdio.h>int main(){ int i=4; switch(i) { default: printf("This is default\n"); case 1: printf("This is case 1\n"); break; case 2: printf("This is case 2\n"); break; case 3: printf("This is case 3\n"); } return 0;}

A.This is defaultThis is case 1

B.This is case 3This is default

C.This is case 1This is case 3

D. This is default

View Answer C Compiler Report Discuss in Forum

18. What will be the output of the program?

#include<stdio.h>int main(){ int i = 1; switch(i) { printf("Hello\n"); case 1: printf("Hi\n"); break; case 2: printf("\nBye\n"); break; } return 0;}

A.HelloHi

B.HelloBye

C. Hi D. Bye

View Answer C Compiler Report Discuss in Forum

19. What will be the output of the program?

Page 11: Objective Programs

#include<stdio.h>int main(){ char j=1; while(j < 5) { printf("%d, ", j); j = j+1; } printf("\n"); return 0;}

A. 1 2 3 ... 127

B. 1 2 3 ... 255

C. 1 2 3 ... 127 128 0 1 2 3 ... infinite times

D. 1, 2, 3, 4

View Answer C Compiler Report Discuss in Forum

20. What will be the output of the program?

#include<stdio.h>int main(){ int x, y, z; x=y=z=1; z = ++x || ++y && ++z; printf("x=%d, y=%d, z=%d\n", x, y, z); return 0;}

A. x=2, y=1, z=1 B. x=2, y=2, z=1

C. x=2, y=2, z=2 D. x=1, y=2, z=1

View Answer C Compiler Report Discuss in Forum

Page 12: Objective Programs

1. Point out the error, if any in the for loop.

#include<stdio.h>int main(){ int i=1; for(;;) { printf("%d\n", i++); if(i>10) break; } return 0;}

A. There should be a condition in the for loop

B. The two semicolons should be dropped

C. The for loop should be replaced with while loop.

D. No error

View Answer C Compiler Report Discuss in Forum

Point out errors2. Point out the error, if any in the program.

#include<stdio.h>int main(){ int a = 10; switch(a) { } printf("This is c program."); return 0;}

A. Error: No case statement specified

B. Error: No default specified

C. No Error

D. Error: infinite loop occurs

View Answer C Compiler Report Discuss in Forum

3. Point out the error, if any in the program.

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

Page 13: Objective Programs

int i = 1; switch(i) { printf("This is c program."); case 1: printf("Case1"); break; case 2: printf("Case2"); break; }return 0;}

A. Error: No default specified

B. Error: Invalid printf statement after switch statement

C. No Error and prints "Case1"

D. None of above

View Answer C Compiler Report Discuss in Forum

4. Point out the error, if any in the while loop.

#include<stdio.h>int main(){ int i=1; while() { printf("%d\n", i++); if(i>10) break; } return 0;}

A. There should be a condition in the while loop

B. There should be at least a semicolon in the while

C. The while loop should be replaced with for loop.

D. No error

View Answer C Compiler Report Discuss in Forum

5. Which of the following errors would be reported by the compiler on compiling the program given below?

#include<stdio.h>int main()

Page 14: Objective Programs

{ int a = 5; switch(a) { case 1: printf("First");

case 2: printf("Second");

case 3 + 2: printf("Third");

case 5: printf("Final"); break;

} return 0;}

A. There is no break statement in each case.

B. Expression as in case 3 + 2 is not allowed.

C. Duplicate case case 5:

D. No error will be reported.

View Answer C Compiler Report Discuss in Forum

6. Point out the error, if any in the program.

#include<stdio.h>int main(){ int P = 10; switch(P) { case 10: printf("Case 1");

case 20: printf("Case 2"); break;

case P: printf("Case 2"); break; } return 0;}

A. Error: No default value is specified

B. Error: Constant expression required at line case P:

C. Error: There is no break statement in each case.

Page 15: Objective Programs

D. No error will be reported.

View Answer C Compiler Report Discuss in Forum

7. Point out the error, if any in the program.

#include<stdio.h>int main(){ int i = 1; switch(i) { case 1: printf("Case1"); break; case 1*2+4: printf("Case2"); break; }return 0;}

A. Error: in case 1*2+4 statement

B. Error: No default specified

C. Error: in switch statement

D. No Error

View Answer C Compiler Report Discuss in Forum

8. Point out the error, if any in the while loop.

#include<stdio.h>int main(){ void fun(); int i = 1; while(i <= 5) { printf("%d\n", i); if(i>2) goto here; }return 0;}void fun(){ here: printf("It works");}

A. No Error: prints "It works"

B. Error: fun() cannot be accessed

Page 16: Objective Programs

C. Error: goto cannot takeover control to other function

D. No error

View Answer C Compiler Report Discuss in Forum

9. Point out the error, if any in the program.

#include<stdio.h>int main(){ int a = 10, b; a >=5 ? b=100: b=200; printf("%d\n", b); return 0;}

A. 100 B. 200

C. Error: L value required for b D. Garbage value

View Answer C Compiler Report Discuss in Forum

Page 17: Objective Programs

Point out correct statements

1. Which of the following statements are correct about the below program?

#include<stdio.h>int main(){ int i = 10, j = 20; if(i = 5) && if(j = 10) printf("Have a nice day"); return 0;}

A. Output: Have a nice day

B. No output

C. Error: Expression syntax

D. Error: Undeclared identifier if

View Answer C Compiler Report Discuss in Forum

2. Which of the following statements are correct about the below program?

#include<stdio.h>int main(){ int i = 10, j = 15; if(i % 2 = j % 3) printf("IndiaBIX\n"); return 0;}

A. Error: Expression syntax B. Error: Lvalue required

C. Error: Rvalue required D. The Code runs successfully

View Answer C Compiler Report Discuss in Forum

3. Which of the following statements are correct about the program?

#include<stdio.h>int main(){ int x = 30, y = 40; if(x == y) printf("x is equal to y\n");

else if(x > y) printf("x is greater than y\n");

else if(x < y) printf("x is less than y\n")

Page 18: Objective Programs

return 0;}

A. Error: Statement missing B. Error: Expression syntax

C. Error: Lvalue required D. Error: Rvalue required

View Answer C Compiler Report Discuss in Forum

4. Which of the following statements are correct about an if-else statements in a C-program?

1: Every if-else statement can be replaced by an equivalent statements using ?: operators

2: Nested if-else statements are allowed.

3: Multiple statements in an if block are allowed.

4: Multiple statements in an else block are allowed.

A. 1 and 2 B. 2 and 3

C. 1, 2 and 4 D. 2, 3, 4

View Answer C Compiler Report Discuss in Forum

5. Which of the following statements are correct about the below program?

#include<stdio.h>int main(){ int i = 0; i++; if(i <= 5) { printf("IndiaBIX\n"); exit(0); main(); } return 0;}

A. The program prints 'IndiaBIX' 5 times

B. The program prints 'IndiaBIX' one time

C. The call to main() after exit() doesn't materialize.

D. The compiler reports an error since main() cannot call itself.

View Answer C Compiler Report Discuss in Forum

6. Which of the following statements are correct about the below C-program?

#include<stdio.h>

Page 19: Objective Programs

int main(){ int x = 10, y = 100%90, i; for(i=1; i<10; i++) if(x != y); printf("x = %d y = %d\n", x, y); return 0;}

1 : The printf() function is called 10 times.

2 : The program will produce the output x = 10 y = 10

3 : The ; after the if(x!=y) will NOT produce an error.

4 : The program will not produce output.

A. 1 B. 2, 3

C. 3, 4 D. 4

View Answer C Compiler Report Discuss in Forum

7. Which of the following sentences are correct about a for loop in a C program?

1: for loop works faster than a while loop.

2: All things that can be done using a for loop can also be done using a whileloop.

3: for(;;); implements an infinite loop.

4: for loop can be used if we want statements in a loop get executed at least once.

A. 1 B. 1, 2

C. 2, 3 D. 2, 3, 4

View Answer C Compiler Report Discuss in Forum

8. Which of the following statements are correct about the below program?

#include<stdio.h>int main(){ int n = 0, y = 1; y == 1 ? n=0 : n=1; if(n) printf("Yes\n"); else printf("No\n"); return 0;}

A. Error: Declaration terminated incorrectly

B. Error: Syntax error

C. Error: Lvalue required

Page 20: Objective Programs

D. None of above

View Answer C Compiler Report Discuss in Forum

9. Which of the following sentences are correct about a switch loop in a C program?

1: switch is useful when we wish to check the value of variable against a particular set of values.

2: switch is useful when we wish to check whether a value falls in different ranges.

3: Compiler implements a jump table for cases used in switch.

4: It is not necessary to use a break in every switch statement.

A. 1,2 B. 1,3,4

C. 2,4 D. 2

View Answer C Compiler Report Discuss in Forum

Page 21: Objective Programs

Write a C program to find larger number between two numbers entered by user.

#include <stdio.h>int main(){ float a,b; printf("Enter two numbers: "); scanf("%f%f",&a,&b); if (a>b) printf("%.2f is larger.",a); else printf("%.2f is larger.",b); return 0;}

Write a C program to find larger integer among 4 integers entered by user

#include <stdio.h>int main(){ int a,b,c,d; printf("Enter 4 integers: "); scanf("%d%d%d%d",&a,&b,&c,&d); if (a>b && a>c && a>d) printf("%d is largest integer.",a); else if (b>a && b>c && b>d) printf("%d is larger integer.",b); else if (c>a && c>b && c>d) printf("%d is larger integer.",c); else printf("%d is largest integer.",d); return 0;}

Write a C program to make a simple calculator using switch...case statements.

#include <stdio.h>int main(){ char operator; float num1,num2; printf("Enter operator +, - , * or / :\n"); operator=getche(); printf("\nEnter two operands:\n"); scanf("%f%f",&num1,&num2); switch(operator) { case '+': printf("num1+num2=%.2f",num1+num2); break; case '-': printf("num1-num2=%.2f",num1-num2);

Page 22: Objective Programs

break; case '*': printf("num1*num2=%.2f",num1*num2); break; case '/': printf("num2/num1=%.2f",num1/num2); break; default: printf(Error! operator is not correct"); break; } return 0;}

Write a C program to check whether a number entered by user is prime or not.

#include <stdio.h>int main(){ int num,i,flag=0; printf("Enter number to check whether it is prime or not\n"); scanf("%d",&num); for(i=2;i<=(num/2);++i){ if((num%i)==0){ printf("%d in not prime.",num); flag=1; break; } } if(flag==0) printf("%d is prime.",num); return 0;}

Write a C program to check whether a number is Armstrong number of not.

#include <stdio.h>int main(){ int num,i,sum=0,remain,temp; printf("Enter a number to check: "); scanf("%d",&num); temp=num; while(num!=0){ remain=(num%10); sum+=remain*remain*remain; num=num/10; } if(sum==temp) printf("%d is Armstrong\n",temp);

Page 23: Objective Programs

else printf("%d is not Armstrong\n",temp); return 0;}

Write a C program to print the multiple of a number, where number is entered by a

user.(Find multiple upto 10)

#include <stdio.h>int main(){ int num,i; printf("Enter a number to find multiple.\n"); scanf("%d",&num); for(i=1;i<=10;++i){ printf("%d*%d=%d\n",num,i,num*i); } return 0;}

Write a C program to display the characters from A to Z using loops. (Assume, you don't

know the ASCII value of alphabets.)

#include <stdio.h>int main(){ int c; for(c='A';c<='Z';++c) printf("%c ",c); return 0;}

Write a C program to display all the prime numbers between two numbers entered by a

user.

#include <stdio.h>int main(){ int n1,n2,temp,i,j,flag; printf("Enter two numbers\n"); scanf("%d%d",&n1,&n2); if(n1>n2){ temp=n1; n1=n2; n2=temp; }

Page 24: Objective Programs

printf("List of prime numbers between %d and %d are:\n",n1,n2); for(i=n1+1;i<n2;++i){ flag=0; for(j=2;j<=i/2;++j){ if(i%j==0){ flag=1; break; } } if(flag==0) printf("%d ",i); } return 0;}

Write a C program to display a Pascal's triangle using loops. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

#include <stdio.h>int main(){ int c,i,j,k,h; printf("Enter the vertical height of Pascal's triangle"); scanf("%d",&h); for(i=0;i<h;i++) { c=1; for(j=1;j<=2*(h-1-i);j++) printf(" "); for(k=0;k<=i;k++) { printf("%3d ", c); c=c*(i-k)/(k+1); } printf("\n"); } return 0;}

Write a C program to check for, whether a number is a prime number or Armstrong

number or lies on Fibonacci sequence according to instruction from user using

switch...case statement.

#include <stdio.h>int main(){ char check;

Page 25: Objective Programs

int i, flag, num,temp,sum=0; printf("Enter number: "); scanf("%d",&num); printf("-----------------------Instructions--------\n"); printf("Enter 'a' to check for Armstrong number.\n"); printf("Enter 'p' to check for prime number.\n"); printf("Enter 'f' to check for Fibonacci sequence.\n"); printf("------------------------------------------\n"); printf("Enter a character according to instruction specified: "); check=getche(); switch(check) { case 'a': temp=num; while(num!=0){ sum+=(num%10)*(num%10)*(num%10); num=num/10; } if(sum==temp) printf("\n\nResult: %d is Armstrong\n",temp); else printf("\n\nResult: %d is not Armstrong\n",temp); break; case 'p': flag=0; for(i=2;i<=(num/2);++i){ if((num%i)==0){ printf("\n\nResult: %d in not prime.",num); flag=1; break; } } if(flag==0) printf("\n\nResult: %d is prime.",num); break; case 'f': i=0,flag=1; while(flag<=num) { if (flag==num){ printf("\n\nResult: %d lies in Fibonacci.",num); break; } temp=flag; flag+=i; i=temp; } if (flag!=num) printf("\n\nResult: %d does not lies in Fibonacci.",num); break; default: printf("Error! "); break; } return 0;}

Page 26: Objective Programs