Aptitute question papers in c
Upcoming SlideShare
Loading in...5
×
 

Aptitute question papers in c

on

  • 1,990 views

Placement Papers are published here to help freshers to get themselves prepared for the tests and successfully procure jobs in top companies of India including Infosys, Wipro, TCS, HCL, HP, Accenture, ...

Placement Papers are published here to help freshers to get themselves prepared for the tests and successfully procure jobs in top companies of India including Infosys, Wipro, TCS, HCL, HP, Accenture, CTS etc. This is the section where you can find previous and latest questions, practice them with the solutions in hand.

Statistics

Views

Total Views
1,990
Views on SlideShare
1,990
Embed Views
0

Actions

Likes
1
Downloads
266
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Aptitute question papers in c Aptitute question papers in c Document Transcript

  • C ProgramsTYPEDEF1. What can be said of the following program ?main(){enum Months {JAN =1,FEB,MAR,APR};Months X = JAN;if(X==1){printf("Jan is the first month");}}a) Does not print anythingb) Prints : Jan is the first monthc) Generates compilation errord) Results in runtime errorAnswer : b2.main() { extern int i; i=20; printf("%d",i); } Answer: Linker Error : Undefined symbol _i Explanation:extern storage class in the following declaration, extern int i;specifies to the compiler that the memory for iis allocated in some other program and that addresswill be given to the current program at the time of linking. But linker finds that no other variable ofname iis available in any other program with memory space allocated for it. Hence a linker error hasoccurred .3. enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined.PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • 4. Given the following statement enum day = { jan = 1 ,feb=4, april, may} What is the valueof may?(a) 4(b) 5(c) 6(d) 11(e) None of the aboveAnswer c) 6SWITCH:5. What is the output of the following program?main(){int l=6;switch(l){ default : l+=2;case 4: l=4;case 5: l++;break;}printf("%d",l);}a)8 b)6 c)5 d)4 e)noneAnswer : c) 5 6. main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } } Answer : three Explanation : The default case can be placed anywhere inside the loop. It is executed only when allother cases doesnt match.7. #include<stdio.h> main() { int i=1,j=2; switch(i) {PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer: Compi ler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements.8. main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } Answer: Compi ler Error: switch expression not integral Explanation: Switch statements can be applied only to integral types.10. Output of the following program ismain(){ int i=0; for(i=0;i<20;i++) { switch(i) case 0:i+=5; case 1:i+=2; case 5:i+=5; default i+=4; break; } printf("%d,",i); }}a) 0,5,9,13,17b) 5,9,13,17c) 12,17,22d) 16,21e) Syntax errorAns. (d)PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • 11. main(){int i;for(i=0;i<3;i++)switch(i){case 1: printf("%d",i);case 2 : printf("%d",i);default: printf("%d"i);}} Answer: 011122FUNCTIONS:12. What is the output of the following program?main(){int x=20;int y=10;swap(x,y);printf("%d %d",y,x+2);}swap(int x,int y){int temp;temp =x;x=y;y=temp;} a) 10,20 b) 20,12 c) 22,10 d)10,22 e)noneAnswer : d)10,2213. Which of the following about the following two declaration is truei ) int *F()ii) int (*F)() Choice :a) Both are identicalb) The first is a correct declaration and the second is wrongc) The first declaraion is a function returning a pointer to an integer and the second is apointer to function returning intd) Both are different ways of declarin pointer to a function Answer : c). 14. main() { printf("%p",main); }PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses).main() is also a function. So the address of function main will be printed. %p in printf specifies thatthe argument is an address. They are printed as hexadecimal numbers.15. main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the second clrscr(); is a function declaration (because it is not inside any function).16. main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 Explanation:Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which shouldhave been scanned successfully. So number of items read is 1.17. main() { show(); } void show() { printf("Im the greatest"); } Answer: Compi er error: Type mismatch in redeclaration of show. Explanation: When the compiler sees the function show it doesnt know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error. The solutions are as follows: 1. declare void show() in main() . 2. define show() before main(). 3. declare extern void show() before the use of show().18. main() { main(); }PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • Answer: Runtime error : Stack overflow. Explanation: main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.19. What are the following notations of defining functions known as? i. int abc(int a,float b){/* some code */ } ii. int abc(a,b) int a; float b;{/* some code*/} Answer: i. ANSI C notati on ii. Kernighan & Ritche notation 20. What is printed when this program is executed main() { printf ("%dn",f(7)); } f(X) { if ( x<= 4) return x; return f(--x); } a) 4 b)5 c) 6 d) 7 Answer : a) 21. what is printed when the following program is compiled and executed? int func (int x) { if (x<=0) return(1); return func(x -1) +x; } main()PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • { printf("%dn",func(5)); } a) 12 b) 16 c) 15 d) 11 Answer : .b) 16. 22. Find the out put: main() { int a==4sqrt(a);printf("%d",a); }1).2.0 2). 2 3). 4.0 4). 4 Answer : 223. Find the outputmain(){int a[]={ 2,4,6,8,10 };int i; change(a,5); for( i = 0; i <= 4; i++) printf("n %d",a[i]); } change( int *b, int n) { int i; for( i = 0; i < n; i++) *(b+i) = *(b+i) + 5; }Answer:24. #include<studio.h>main(){func(1);}func(int i){static char *str[] ={ "One","Two","Three","Four"};printf("%sn",str[i++]);return;}Answer:- it will give warning because str is pointer to the char butit is initialized with more valuesif it is not considered then the answer is Two */PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • 25. Find the out put:#include<stdio.h>/* This problem was asked in PCS Bombay in a walk-in-interviewWrite a recursive function that calculatesn * (n-1) * (n-2) * ....... 2 * 1 */main() {int factorial(int n);int i,ans;printf("n Enter a Number:");scanf("%d",&i);ans = factorial(i);printf("nFactorial by recursion = %dn", ans);}int factorial(int n){if (n <= 1) return (1);elsereturn ( n * factorial(n-1));}Answer :26.Find the output#include <stdio.h>main(){int j,ans;j = 4;ans = count(4);printf("%dn",ans);}int count(int i){if ( i < 0) return(i);elsereturn( count(i-2) + count(i-1));}Answer :/* It is showing -18 as an answer */ 27. Find the out put int x; main() { int x=0; {PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • int x=10; x++; change_value(x); x++; Modify_value(); printf("First output : %dn",x); } x++; change_value(x); printf("Second Output : %dn",x); Modify_value(); printf("Third Output : %dn",x); } Modify_val ue() { return (x+=10); } change_value() { return(x+=1); } Answer :28. Consider the following programmain(){int i=20,*j=&i;f1(j);*j+=10;f2(j);printf("%d and %d",i,*j);}f1(k)int *k;{*k +=15;}f2(x)int *x;{int m=*x,*n=&m;*n += 10;}The values printed by the program will bea) 20 and 55b) 20 and 45c) 45 and 45PANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • d) 45 and 55e) 35 and 35Answer : c29. what is printed when the following program iscompiled and executed?int func (int x){if (x<=0)return(1);return func(x -1) +x;}main(){printf("%dn",func(5));}a) 12b) 16c) 15d) 11Answer : bSTRUCTURE AND UNION:30. What is the size of the following union. Assume that the size of int =2, size of float =4 and size ofchar =1.Union Tag{int a;float b;char c;};a)2 b)4c)1 d) 7Answer : bDATA TYPES 31. What is th output of the fol lowing program?int x= 0x65;main(){char x;printf("%dn",x)}a) compilation error b) A c) 65 d) unidentifiedPANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com
  • Answer : cPANTECH SOLUTIONS PVT LTD , 151 / 34 , Sri Renga Building , T Nagar , Chennai – 17 Tel : 044 42606450 / 96000 66689 Branches @ Madurai , Coimbatore, Cochin , Pune, Hyderabad and Tirunelveli Pantech Potency : Final Year IEEE / Non IEEE Projects and Professional Courses. www.pantechprojects.com