You are on page 1of 23

ANSWERS

C QUESTIONS:

1. The C language terminator is a.semicolon b.colon

c.period

d.exclamation mark

2. What is false about the following: A compound statement is a.A set of simple statements b.Demarcated on either side by curly brackets c.Can be used in place of simple statement d. A C function is not a compound statement.

3. What is true about the following about C Functions a.Need not return any value b.Should always return an integer c.Should always return a float d.Should always return more than one value. 4. Main must be written as a.the first function in the program c.Last function in the program

b.Second function in the program d.any where in the program

5. Which of the following about automatic variables within a function is correct ? a.its type must be declared before using the variable b.they are local c.they are not initialised to zero d.they are global. 6. Write one statement equivalent to the following two statements x=sqr(a); return(x); Choose from one of the alternatives a.return(sqr(a)); b.printf("sqr(a)"); c.return(a*a*a); d.printf("%d",sqr(a)); 7. Which of the following about the C comments is incorrect? a.commentscan go over multiple lines b.comments can start any where in the line c.a line can contain comments with out any language statements d.comments can occur within comments 8. What is the value of y in the following code?
1 T&P, SASTRA-SRC

ANSWERS

x=7;y=0; if(x=6) else a.7

y=7; y=1; b.0

c.1

d.6

9. Read the function conv() given below conv(int t) { int u; u=5/9 * (t-32); return(u); a.15 b.0 c.16.1 d.29

10. Which of the following represents true statement either x is inthe range of 10 and 50 or y is zero a.x>=10 && x<=50 || y==0; b.x<=10 && x>=50 || y=1 c.x<10 && x=50 || y=0 d.x=10 && x<=50 || y=0 11. Which of the following is not an infinite loop ? a.while(1) { .... } b.for(;;){ ... } c.x=0; do{ /*x unaltered within theloop*/ ... }while(x==0); d.# define TRUE 0 ... while(TRUE){ .... } 12. What does the following function print? func(int i)
2 T&P, SASTRA-SRC

ANSWERS

{ if(i%2)return 0; eale return 1; } main() { int =3; i=func(i); i=func(i); printf("%d",i); } a.3 b.1 c.0

d.2

13. How does the C compiler interpret the following two statements p=p+x; q=q+y; a.p=p+x; q=q+y b.p=p+xq=q+y c.p=p+xq; q=q+y d.p=p+x/q=q+y For questions 14,15,16,17 use the following alternatives a.int b.char c.string d.float 14. '9' 15. "1 e 02" 16. 10e05 17. 15 18. Read the folllowing code # define MAX 100 # define MIN 100 .... .... if(x>MAX) x=1; else if(x<MIN) x=-1; x=50; if the initial value of x=200,what is the vlaue after executing this code? a.200 b.1 c.-1 d.50
3 T&P, SASTRA-SRC

ANSWERS

19. A memory of 20 bytes is allocated to a string declared as char *s then the following two statements are executed: s="Etrance" l=strlen(s); what is the value of l ? a.20 b.8 c.9 d.21 20. Given the piece of code int a[50]; int *pa; pa=a; to access the 6th element of the array which of the following is incorrect? a.*(a+5) b.a[5] c.pa[5] d.*(*pa + 5) 21. Consider the following structure: struct num nam{ int no; char name[25]; }; struct num nam n1[]={{12,"Fred"},{15,"Martin"},{8,"Peter"},{11,Nicholas"}}; ..... ..... printf("%d%d",n1[2],no,(*(n1 + 2),no) + 1); What does the above statement print? a.8,9 b.9,9 c.8,8 d.8,unpredictable value 22.Identify the in correct expression float b; a=b=3.5; a.a=b=3=4; b.a=b=c=d=0; c.float a=int b=3.5;d.int a; 23. Regarding the scope of the varibles;identify the incorrect statement: a.automatic variables are automatically initialised to 0 b.static variables are are automatically initialised to 0 c.the address of a register variable is not accessiable d.static variables cannot be initialised with any expression 24. cond 1?cond 2?cond 3?:exp 1:exp 2:exp 3:exp 4; is equivalent to which of the following? a.if cond 1 exp 1;
4 T&P, SASTRA-SRC

ANSWERS

else if cond 2 exp 2; else if cond 3 exp 3; else exp 4; b.if cond 1 if cond 2 if cond 3 exp 1; else exp 2; else exp 3; else exp 4; c.if cond 1 && cond 2 && cond 3 exp 1 |exp 2|exp 3|exp 4; d.if cond 3 exp 1; else if cond 2 exp 2; else if cond 3 exp 3; else exp 4; 25. The operator for exponencation is a.** b.^ c.% d.not available 26. Which of the following is invalid a.a+=b b.a*=b c.a>>=b 27. What is y value of the code if input x=10 y=5; if (x==10) else if(x==9) else y=8; a.9 b.8 c.6 d.7 28. What does the following code do? fn(int n,int p,int r) { static int a=p; switch(n){ case 4:a+=a*r; case 3:a+=a*r; case 2:a+=a*r; case 1:a+=a*r; } }
5 T&P, SASTRA-SRC

d.a**=b

ANSWERS

a.computes simple interest for one year b.computes amount on compound interest for 1 to 4 years c.computes simple interest for four year d.computes compound interst for 1 year 29. a=0; while(a<5) printf("%d\n",a++); how many times does the loop occurs? a.infinite b.5 c.4 d.6 30. How many times does the loop iterated ? for (i=0;i=10;i+=2) printf("Hi\n"); a.10 b.2 c.5 d.12 31. What is incorrect among teh following A recursive functiion a.calls itself b.is equivalent to a loop c.has a termination cond d.does not have a return value at all 32. Which of the following go out of the loopo if expn 2 becoming false a.while(expn 1){...if(expn 2)continue;} b.while(!expn 1){if(expn 2)continue;...} c.do{..if(expn 1)continue;..}while(expn 2); d.while(!expn 2){if(expn 1)continue;..} 33. Consider the following program main() {unsigned int i=10; while(i>=0){ printf("%u",i) i--; } } how many times the loop executed a.10 b.9 c.11 34. Pick out the odd one out a.malloc() b.calloc() c.free()
6

d.infinite

d.realloc()
T&P, SASTRA-SRC

ANSWERS

35. Consider the following program main() { int a[5]={1,3,6,7,0}; int *b; b=&a[2]; } the value of b[-1] is a.1 b.3 c.-6 36. # define prod(a,b)=a*b main() { int x=2; int y=3; printf("%d",prod(x+2,y-10)); } the output of the program is a.8 b.6 c.7

d.none

d.none

37. Consider the following program sigment int n,sum=1; switch(n) { case 2:sum=sum+2; case 3:sum*=2; break; default:sum=0;} if n=2, what is the value of sum a.0 b.6 c.3 d.none 38. Identify the incorrect one 1.if(c=1) 2.if(c!=3) 3.if(a<b)then 4.if(c==1) a.1 only b.1&3 c.3 only

d.all

39.The format specified for hexa decimal is a.%d b.%o c.%x d.%u

T&P, SASTRA-SRC

ANSWERS

40. Find the output of the following program main() { int x=5, *p; p=&x; printf("%d",++*p); } a.5 b.6 c.0 d.none 41. Consider the following C code main() { int i=3,x; while(i>0) { x=func(i); i--; } int func(int n) { static sum=0; sum=sum+n; return(sum); } the final value of x is a.6 b.8 c.1 43. int *a[5] refers to a.array of pointers c.pointer to a pointer

d.3

b.pointer to an array d.array to a pointer

46. Which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT; 47) main() { int a=0;
8 T&P, SASTRA-SRC

ANSWERS

if(a==0) printf("Cisco Systems\n"); printf("Cisco Systems\n"); } a. Compiler error b. Runtime error c. Cisco System d. No output 48) main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } } a.Compiler Error: switch expression not integral c. 0 d.none of the above 49) main() { extern i; printf("%d\n",i); { int i=20; printf("%d\n",i); } } a.Compiler error c.Run time error

b. linker error

b.Linker Error : Unresolved external symbol i d.No output

50) main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }


9 T&P, SASTRA-SRC

ANSWERS

a.15 15 15

b.12 12 12

c.Error

d.16 16 16

PART-2
1)Which is the parameter that is added to every non-static member function when it is called? 2) What is C language? 3)What does static variable mean? 4)Compilation How to reduce a final size of executable? 5)Linked Lists -- Can you tell me how to check whether a linked list is circular? 6)Write down the equivalent pointer expression for referring the same element a[i][j][k][l]? 7)Which bit wise operator is suitable for checking whether a particular bit is on or off? 8)Which bit wise operator is suitable for turning off a particular bit in a number? 9)Which bit wise operator is suitable for putting on a particular bit in a number? 10) What is an argument ? differentiate between formal arguments and actual arguments? 11)What are advantages and disadvantages of external storage class? 12) When is a switch statement better than multiple if statements? 13)What is storage class and what are storage variable ? 14)What is a static function? 15) What are the different storage classes in C ?
10 T&P, SASTRA-SRC

ANSWERS

16) Can static variables be declared in a header file? 17) Can a variable be both const and volatile? 18) What is a null pointer? 19) Write a program to interchange 2 variables without using the third one. 20) How do you override a defined macro? 21) When does the compiler not implicitly generate the address of the first element of an array? 22) When should a type cast be used? 23) What is the purpose of realloc( )? 24) What is the purpose of main( ) function? 25) Why n++ executes faster than n+1? 26) What's the difference between these two declarations? struct x1 { ... }; typedef struct { ... } x2; 27) Why doesn't struct x { ... }; x thestruct; work? 28) Can a structure contain a pointer to itself? 29) What's the best way of implementing opaque (abstract) data types in C? 30) I came across some code that declared a structure like this: struct name { int namelen; char namestr[1];
11 T&P, SASTRA-SRC

ANSWERS

}; and then did some tricky allocation to make the namestr array act like it had several elements. Is this legal or portable? 31) How can I pass constant values to functions which accept structure arguments? 32) How can I access structure fields by name at run time? 33) What is #line used for? 34) What is static memory allocation and dynamic memory allocation? 35) How are pointer variables initialized? 36) Difference between arrays and pointers? 37) What are the advantages of the functions? 38) Is NULL always defined as 0? 39) What is the difference between NULL and NUL? 40) Can the sizeof operator be used to tell the size of an array passed to a function? 41) Is using exit() the same as using return? 42) Can math operations be performed on a void pointer? 43) Are pointers integers? 44) What is a method? 45) How can you determine the maximum value that a numeric variable can hold? 46) How many levels deep can include files be nested?

12

T&P, SASTRA-SRC

ANSWERS

47) What is the difference between declaring a variable and defining a variable? 48) How can I sort a linked list? 49) What does it mean when a pointer is used in an if statement? 50) Diffenentiate between an internal static and external static variable?

13

T&P, SASTRA-SRC

BACK

Answers:
PART-1 1. D 2. C 3. D 4. C 5. C 6. D 7. C 8. C 9. A 10. D 11. D 12. C 13. A 14. A 15. A 16. B 17. C 18. D 19. C 20. A 21. C 22. D 23. B 24. D 25. A 26. C 27. B 28. B 29. A 30. D 31. B 32. C 33. C 34. C
14 T&P, SASTRA-SRC

BACK

35. B 36. A 37. B 38. A 39. B 40. B 41. A 42. D 43. D 44. D 45. A 46. B 47. C 48. A 49. B 50. D PART-2 1) this pointer 2) The C programming language is a standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the UNIX operating system. It has since spread to many other operating systems, and is one of the most widely used programming languages. C is prized for its efficiency, and is the most popular programming language for writing system software, though it is also used for writing applications 3) there are 3 main uses for the static. 1. If you declare within a function: It retains the value between function calls 2.If it is declared for a function name: By default function is extern..so it will be visible from other files if the function declaration is as static..it is invisible for the outer files 3. Static for global variables: By default we can use the global variables from outside files If it is static global..that variable is limited to with in the file 4) Size of the final executable can be reduced using dynamic linking for libraries
15 T&P, SASTRA-SRC

5) Create two pointers, and set both to the start of the list. Update each as follows: while (pointer1) { pointer1 = pointer1->next; pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next; if (pointer1 == pointer2) { print ("circular"); } } If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, its either 1 or 2 jumps until they meet. 6) a[i] == *(a+i) a[i][j]== *(*(a+i)+j) a[i][j][k] == *(*(*(a+i)+j)+k) a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l) 7)The bitwise AND operator. Here is an example:enum { KBit0 = 1, KBit1, KBit31, }; if ( some_int & KBit24 ) printf ( Bit number 24 is ON\n ); else printf ( Bit number 24 is OFF\n ); 8) The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero. some_int = some_int & ~KBit24; 9) The bitwise OR operator. In the following code snippet, the bit number 24 is turned ON: some_int = some_int | KBit24;

16

T&P, SASTRA-SRC

10) An argument is an entity used to pass the data from calling function to the called function. Formal arguments are the arguments available in the function definition. They are preceded by their own data types. Actual arguments are available in the function call. 11) Advantages of external storage class 1) Persistent storage of a variable retains the latest value 2)The value is globally available Disadvantages of external storage class 1)The storage for an external variable exists even when the variable is not needed 2)The side effect may produce surprising output 3)Modification of the program is difficult 4)Generality of a program is affected 12) A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type. 13) A storage class is an attribute that changes the behavior of a variable. It controls the lifetime, scope and linkage. There are five types of storage classes 1) auto 2) static 3) extern 4) register 5) typedef 14) A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope 15) C has three types of storage: automatic, static and allocated. Variable having block scope and without static specifier have automatic storage duration. Variables with block scope, and with static specifier have static scope. Global variables (i.e, file scope) with or without the the static specifier also have static scope.

17

T&P, SASTRA-SRC

Memory obtained from calls to malloc(), alloc() or realloc() belongs to allocated storage class 16) You cant declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended. 17) Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. For instance, in the example in FAQ 8, the timer structure was accessed through a volatile const pointer. The function itself did not change the value of the timer, so it was declared const. However, the value was changed by hardware on the computer, so it was declared volatile. If a variable is both const and volatile, the two modifiers can appear in either order. 18) There are times when its necessary to have a pointer that doesnt point to anything. The macro NULL, defined in , has a value thats guaranteed to be different from any valid pointer. NULL is a literal zero, possibly cast to void* or char*. Some people, notably C++ programmers, prefer to use 0 rather than NULL. The null pointer is used in three ways: 1) To stop indirection in a recursive data structure 2) As an error value 3) As a sentinel value 19)a=7; b=2; a = a + b; b = a - b; a = a - b; 20) You can use the #undef preprocessor directive to undefine (override) a previously defined macro. 21) Whenever an array name appears in an expression such as - array as an operand of the sizeof operator - array as an operand of & operator
18 T&P, SASTRA-SRC

- array as a string literal initializer for a character array Then the compiler does not implicitly generate the address of the address of the first element of an array. 22) There are two situations in which to use a type cast. The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly. The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers. For example, the following line type casts the return value of the call to malloc() to be a pointer to a foo structure. struct foo *p = (struct foo *) malloc(sizeof(struct foo)); 23) The function realloc(ptr,n) uses two arguments.the first argument ptr is a pointer to a block of memory for which the size is to be altered. The second argument n specifies the new size. The size may be increased or decreased. If n is greater than the old size and if sufficient space is not available subsequent to the old region, the function realloc( ) may create a new region and all the old data are moved to the new region. 24) The function main( ) invokes other functions within it.It is the first function to be called when the program starts execution. - It is the starting function - It returns an int value to the environment that called the program - Recursive call is allowed for main( ) also. - It is a user-defined function - Program execution ends when the closing brace of the function main( ) is r reached. - It has two arguments 1)argument count and 2) argument vector (represents strings passed). - Any user-defined name can also be used as parameters for main( ) instead of argc and argv 25) The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation 26) The first form declares a "structure tag"; the second declares a "typedef". The main difference is that the second declaration is of a slightly more abstract type -- its users don't
19 T&P, SASTRA-SRC

necessarily know that it is a structure, and the keyword struct is not used when declaring instances of it. 27) C is not C++. Typedef names are not automatically generated for structure tags.

28) Most certainly. 29) One good way is for clients to use structure pointers (perhaps additionally hidden behind typedefs) which point to structure types which are not publicly defined. This technique is popular, although Dennis Ritchie has called it "unwarranted chumminess with the C implementation." An official interpretation has deemed that it is not strictly conforming with the C Standard. (A thorough treatment of the arguments surrounding the legality of the technique is beyond the scope of this list.) It does seem to be portable to all known implementations. (Compilers which check array bounds carefully might issue warnings.) Another possibility is to declare the variable-size element very large, rather than very small; in the case of the above example: ... char namestr[MAXSIZE]; ... where MAXSIZE is larger than any name which will be stored. However, it looks like this technique is disallowed by a strict interpretation of the Standard as well. C has no way of generating anonymous structure values. You will have to use a temporary structure variable or a little structurebuilding function. Build a table of names and offsets, using the offsetof() macro. The offset of field b in struct a is offsetb = offsetof(struct a, b) If structp is a pointer to an instance of this structure, and field b is an int (with offset as computed above), b's value can be set indirectly with *(int *)((char *)structp + offsetb) = value;
20 T&P, SASTRA-SRC

30)

31)

32)

33) The #line preprocessor directive is used to reset the values of the _ _LINE_ _ and _ _FILE_ _ symbols, respectively. This directive is commonly used in fourth-generation languages that generate C language source files. 34) Static memory allocation: The compiler allocates the required memory space for a declared variable.By using the address of operator,the reserved address is obtained and this address may be assigned to a pointer variable.Since most of the declared variable have static memory,this way of assigning pointer value to a pointer variable is known as static memory allocation. memory is assigned during compilation time. Dynamic memory allocation: It uses functions such as malloc( ) or calloc( ) to get memory dynamically.If these functions are used to get memory dynamically and the values returned by these functions are assingned to pointer variables, such assignments are known as dynamic memory allocation.memory is assined during run time. 35) Pointer variable are initialized by one of the following two ways - Static memory allocation - Dynamic memory allocation 36) - Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them - Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression. 37) - Debugging is easier - It is easier to understand the logic involved in the program - Testing is easier - Recursive call is possible - Irrelevant details in the user point of view are hidden in functions - Functions are helpful in generalizing the program 38) NULL is defined as either 0 or (void*)0. These values are almost identical; either a literal zero or a void pointer is converted automatically to any kind of pointer, as necessary, whenever a pointer is needed (although the compiler cant always tell when a pointer is needed).

21

T&P, SASTRA-SRC

39) NULL is a macro defined in for the null pointer. NUL is the name of the first character in the ASCII character set. It corresponds to a zero value. Theres no standard macro NUL in C, but some people like to define it. The digit 0 corresponds to a value of 80, decimal. Dont confuse the digit 0 with the value of (NUL)! NULL can be defined as ((void*)0), NUL as . 40) No. Theres no way to tell, at runtime, how many elements are in an array parameter just by looking at the array parameter itself. Remember, passing an array to a function is exactly the same as passing a pointer to the first element. 41) No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar. 42) No. Pointer addition and subtraction are based on advancing the pointer by a number of elements. By definition, if you have a void pointer, you dont know what its pointing to, so you dont know the size of what its pointing to. If you want pointer arithmetic to work on raw addresses, use character pointers. 43) No, pointers are not integers. A pointer is an address. It is merely a positive number and not an integer. 44) Method is a way of doing something, especially a systematic way; implies an orderly logical arrangement (usually in steps). 45) Which expression always return true? Which always return false? expression if (a=0) always return false expression if (a=1) always return true 46) Even though there is no limit to the number of levels of nested include files you can have, your compiler might run out of stack space while trying to include an inordinately high number of files. This number varies according to your hardware configuration and possibly your compiler.

22

T&P, SASTRA-SRC

47) Declaring a variable means describing its type to the compiler but not allocating any space for it. Defining a variable means declaring it and also allocating space to hold the variable. You can also initialize a variable at the time it is defined. 48) Both the merge sort and the radix sort are good sorting algorithms to use for linked lists 49) Any time a pointer is used as a condition, it means Is this a non-null pointer? A pointer can be used in an if, while, for, or do/while statement, or in a conditional expression.

50) An internal static variable is declared inside a block with static storage
class whereas an external static variable is declared outside all the blocks in a file.An internal static variable has persistent storage,block scope and no linkage.An external static variable has permanent storage,file scope and internal linkage.

23

T&P, SASTRA-SRC

You might also like