Your SlideShare is downloading. ×
0
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
C programming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C programming

6,303

Published on

ICT Training

ICT Training

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,303
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
600
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. ‘C’ Programming<br />
  • 2. Implement strcpy() function.<br />
  • 3. Method1:<br />char *mystrcpy(char *dst, const char *src)<br />{<br /> char *ptr;<br />ptr = dst;<br /> while(*dst++=*src++);<br /> return(ptr);<br />} <br />
  • 4. The strcpy function copies src, including the terminating null character, to thelocation specified by dst. <br />No overflow checking is performed when strings are copied or appended. <br />The behavior of strcpy is undefined if the source and destination strings overlap. <br />It returns the destination string. <br />No return value is reserved to indicate an error. <br />
  • 5. The prototype of strcpy as per the C standards is <br />char *strcpy(char *dst, const char *src);<br />const for the source, signifies that the function must not change the source string <br />
  • 6. Method2:<br />char *my_strcpy(char dest[], const char source[])<br />{<br /> int i = 0;<br /> while (source[i] != '0') <br /> {<br />dest[i] = source[i];<br /> i++;<br /> }<br />dest[i] = '0';<br /> return(dest);<br />}<br />
  • 7. Write C programs to implement the toupper() and the isupper() functions<br />
  • 8. toUpper()<br />isUpper()<br />int toUpper(int ch)<br />{<br /> if(ch>='a' && ch<='z')<br /> return('A' + ch - 'a');<br />else<br /> return(ch);<br />}<br />int isUpper(int ch)<br />{<br /> if(ch>='A' && ch <='Z')<br /> return(1); //Yes, its upper!<br /> else<br /> return(0); // No, its lower!<br />}<br />
  • 9. Write a C program to swap two variables without using a temporary variable <br />
  • 10. Method1: (The XOR)<br />a ^= b ^= a ^= b;<br />Method2: (The XOR)<br />a=a+b;<br />b=a-b;<br />a=a-b;<br />
  • 11. When should a typecast be used? <br />
  • 12. There are two situations in which to use a type cast.<br />The first use is to change the type of an operand to an arithmetic operation so that the operation will be performed properly. <br />The second case is to cast pointer types to and from void * in order to interface with functions that expect or return void pointers.<br />structfoo *p = (structfoo *) malloc(sizeof(structfoo));<br />
  • 13. What do lvalue and rvalue mean?<br />
  • 14. An lvalue is an expression that could appear on the left-hand sign of an assignment (An object that has a location). <br />An rvalue is any expression that has a value (and that can appear on the right-hand sign of an assignment).<br />
  • 15. What is the difference between & and && operators and | and || operators? <br />
  • 16. & and | are bitwise AND and OR operators respectively. They are usually used to manipulate the contents of a variable on the bit level.<br /> && and || are logical AND and OR operators respectively. They are usually used in conditionals. <br />
  • 17. What is sizeof()? <br />Function<br />Operator <br />
  • 18. Function<br />Operator<br />sizeof() is a compile time operator. <br />To calculate the size of an object, we need the type information. <br />
  • 19. How to declare a pointer to a function? <br />
  • 20. int myfunc(); // The function.<br />int (*fp)(); // Pointer to that function.<br />fp = myfunc; // Assigning the address of the function to the pointer.<br />(*fp)(); // Calling the function.<br />fp(); // Another way to call the function. <br />
  • 21. What are the common causes of pointer bugs? <br />
  • 22. Uninitialized pointers: <br />One of the easiest ways to create a pointer bug is to try to reference the value of a pointer even though the pointer is uninitialized and does not yet point to a valid address. <br />For example: <br />int *p;<br />*p = 12;<br />
  • 23. The pointer p is uninitialized and points to a random location in memory when you declare it. <br />It could be pointing into the system stack, or the global variables, or into the program's code space, or into the operating system. <br />When you say *p=12;, the program will simply try to write a 12 to whatever random location p points to.<br />Make sure you initialize all pointers to a valid address before dereferencing them. <br />
  • 24. Invalid Pointer References: <br />An invalid pointer reference occurs when a pointer's value is referenced even though the pointer doesn't point to a valid block. <br />One way to create this error is to say p=q; when q is uninitialized. The pointer p will then become uninitialized as well, and any reference to *p is an invalid pointer reference. <br />The only way to avoid this bug is to draw pictures of each step of the program and make sure that all pointers point somewhere.<br />
  • 25. Zero Pointer Reference: <br />A zero pointer reference occurs whenever a pointer pointing to zero is used in a statement that attempts to reference a block. <br />For example, if p is a pointer to an integer, the following code is invalid: <br />There is no block pointed to by p. Therefore, trying to read or write anything from or to that block is an invalid zero pointer reference. <br />p = 0;<br />*p = 12;<br />
  • 26. How to declare an array of N pointers to functions returning pointers to functions returning pointers to characters? <br />
  • 27. Declare it this way<br />char *(*(*a[N])())();<br />
  • 28. Will C allow passing more or less arguments than required to a function? <br />
  • 29. It will not allow if the prototype is around. <br />It will ideally throw an error like Too many arguments or Too few arguments<br />But if the prototype is not around, the behavior is undefined.<br />
  • 30. Try this out<br />#include <stdio.h><br />int foo(int a);<br />int foo2(int a, int b);<br />int main(int a)<br />{<br /> int (*fp)(int a);<br /> a = foo();<br /> a = foo2(1);<br /> exit(0);<br />}<br />int foo(int a)<br />{<br /> return(a);<br />}<br />int foo2(int a, int b)<br />{<br /> return(a+b);<br />} <br />
  • 31. What does printf() return? <br />
  • 32. Upon a successful return, the printf() function returns the number of characters printed (not including the trailing '0' used to end output to strings). <br />If the output was truncated due to this limit then the return value is the number of characters (not including the trailing '0') which would have been written to the final string if enough space had been available. <br />Thus, a return value of size or more means that the output was truncated. If an output error is encountered, a negative value is returned. <br />
  • 33. Look at the code below. <br />What should X be replaced with inorder to get the output as "Hello World"? <br />void main()<br />{ <br /> if(X) {<br /> printf("Hello");<br />}<br />else {<br /> printf(" World");}<br />}<br />
  • 34. #include <stdio.h><br />int main() <br />{<br /> if(!printf("Hello")) {<br /> printf("Hello");<br />}<br />else {<br /> printf(" World");<br /> }<br />} <br />
  • 35. Write a C program to count bits set in an integer. <br />
  • 36. #include <stdio.h><br />int CountSetBits(int n) {<br /> int count = 0;<br /> while(n) {<br /> n &= (n-1);<br /> ++count;<br /> }<br /> return count;<br />}<br />int main(void)<br />{<br /> int n;scanf("%d",&n);<br /> printf("Number of set bits in %d is %d n", n, CountSetBits(n));<br /> return 0;<br />} <br />
  • 37. Write a C Program to calculate pow(x,n) <br />
  • 38. int pow(int x, int y) {<br /> if(y == 1) return x ;<br /> return x * pow(x, y-1) ;<br />}<br />Divide and Conquer C program<br />#include <stdio.h><br />int main(int argc, char*argv[]) {<br /> printf("n[%d]n",pow(5,4));<br />}<br /> printf("n[%d]n",pow(5,4));<br />}<br />int pow(int x, int n) { if(n==0)return(1);<br /> else if(n%2==0) {<br />return(pow(x,n/2)*pow(x,(n/2)));<br />}<br />else { return(x*pow(x,n/2)*pow(x,(n/2)));<br /> }<br />}<br />
  • 39. Also, the code above can be optimized still by calculating pow(z, (n/2)) only one time (instead of twice) and using its value in the two return() expressions above.<br />
  • 40. Write a code to return a string from a function <br />
  • 41. char *myfunc()<br />{<br /> char *temp = "string";<br /> return temp;<br />}<br />int main()<br />{<br /> puts(myfunc ());<br />} <br />
  • 42. Write code to evaluate a polynomial. <br />
  • 43. typedefstruct node<br />{<br /> float cf;<br /> float px;<br /> float py;<br />struct node *next;<br />}mynode;<br />float evaluate(mynode *head)<br />{<br /> float x,y,sum;<br /> sum = 0;<br />mynode *poly;<br /> for(poly = head->next; poly != head; poly = poly->next)<br />{<br /> sum = sum + poly->cf * pow(x, poly->px) * pow(y, poly->py);<br />} <br /> return(sum);<br />}<br />
  • 44. Write a C program to convert from decimal to any base (binary, hex) <br />
  • 45. #include <stdio.h><br />#include <conio.h><br />void decimal_to_anybase(int,int);<br />void main() {<br />decimal_to_anybase(10, 2);<br />decimal_to_anybase(255, 16);<br /> getch();<br />}<br />void decimal_to_anybase(int n, int base) {<br /> int i, m, digits[1000], flag;<br /> i=0;<br /> printf("nn[%d] converted to base [%d] : ", n, base);<br />
  • 46. while(n) {<br /> m=n%base;<br /> digits[i]="0123456789abcdefghijklmnopqrstuvwxyz"[m];<br /> n=n/base;<br /> i++;<br />}<br />//Eliminate any leading zeroes<br />for(i--;i>=0;i--) {<br /> if(!flag && digits[i]!='0')flag=1;<br /> if(flag)printf("%c",digits[i]);<br /> }<br />} <br />
  • 47. How to fast multiply a number by 7? <br />
  • 48. (num<<3 - num)<br />Same as, num*8 - num = num * (8-1) = num * 7 <br />
  • 49. How can we sum the digits of a given number in single statement? <br />
  • 50. # include<stdio.h><br />void main()<br />{<br /> int num=123456;<br /> int sum=0;<br /> for(;num>0;sum+=num%10,num/=10); // This is the "single line".<br /> printf("nsum = [%d]n", sum);<br />} <br />
  • 51. Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A? <br />
  • 52. #include <stdio.h><br />#include <conio.h><br />int isSubset(char *a, char *b);<br />int main(){<br /> char str1[]="defabc";<br /> char str2[]="abcfed";<br /> if(isSubset(str1, str2)==0){<br /> printf("nYes, characters in B=[%s] are a subset of characters in A=[%s]n",str2,str1); }<br /> else {<br /> printf("nNo, characters in B=[%s] are not a subset of characters in A=[%s]n",str2,str1); }<br /> getch();<br /> return(0); }<br />
  • 53. // Function to check if characters in "b" are a subset<br />// of the characters in "a“<br />int isSubset(char *a, char *b) {<br /> int letterPresent[256];<br /> int i;<br /> for(i=0; i<256; i++)<br />letterPresent[i]=0;<br /> for(i=0; a[i]!='0'; i++)<br />letterPresent[a[i]]++;<br /> for(i=0; b[i]!='0'; i++)<br /> if(!letterPresent[b[i]])<br /> return(1);<br /> return(0);<br />}<br />
  • 54. Write a program to print numbers from 1 to 100 without using loops! <br />
  • 55. void printUp(int startNumber, int endNumber)<br />{<br /> if (startNumber > endNumber)<br /> return;<br /> printf("[%d]n", startNumber++);<br />printUp(startNumber, endNumber);<br />} <br />
  • 56. Write code to round numbers <br />
  • 57. (int)(num < 0 ? (num - 0.5) : (num + 0.5)) <br />
  • 58. How to swap the two nibbles in a byte? <br />
  • 59. #include <stdio.h><br />unsigned char swap_nibbles(unsigned char c) {<br /> unsigned char temp1, temp2;<br /> temp1 = c & 0x0F;<br /> temp2 = c & 0xF0;<br /> temp1=temp1 << 4;<br /> temp2=temp2 >> 4;<br /> return(temp2|temp1); //adding the bits }<br />int main(void) {<br /> char ch=0x34;<br /> printf("nThe exchanged value is %x",swap_nibbles(ch));<br /> return 0; <br />} <br />
  • 60. Write a C program to reverse the words in a sentence in place.<br />Like, <br />Input: I am a good girl<br />Output: should be: lrigdoog a ma I <br />
  • 61. First reverse the whole string and then individually reverse the words<br />#include <stdio.h><br />void rev(char *l, char *r);<br />int main(int argc, char *argv[]){<br /> char buf[] = "the world will go on forever";<br /> char *end, *x, *y;<br />// Reverse the whole sentence first..<br /> for(end=buf; *end; end++);<br /> rev(buf,end-1); <br />
  • 62. // Now swap each word within sentence...<br /> x = buf-1;<br /> y = buf;<br /> while(x++ < end)<br /> {<br /> if(*x == '0' || *x == ' ')<br /> {<br /> rev(y,x-1);<br /> y = x+1;}<br /> }<br />
  • 63. // Now print the final string....<br /> printf("%sn",buf);<br /> return(0); }<br />// Function to reverse a string in place...<br /> void rev(char *l,char *r) {<br /> char t;while(l<r) {<br /> t = *l;<br /> *l++ = *r;<br /> *r-- = t;<br />} }<br />
  • 64. Write a C program to multiply two matrices. <br />
  • 65. // Matrix A (m*n)<br />// Matrix B (n*k)<br />// Matrix C (m*k)<br />for(i=0; i<m; i++) {<br /> for(j=0;j<k;j++) {<br /> c[i][j]=0;<br /> for(l=0;l<n;l++)<br /> c[i][j] += a[i][l] * b[l][j];<br /> }<br />}<br />
  • 66. Is there something we can do in C but not in C++? <br />Declare variable names that are keywords in C++ but not C. <br />
  • 67. #include <stdio.h><br />int main(void)<br />{<br /> int old, new=3;return 0;<br />}<br />
  • 68. Write a C program for calculating the factorial of a number (use recursive function) <br />
  • 69. Here is a recursive C program<br />int fact(int n)<br />{<br /> int fact;<br /> if(n==1)<br /> return(1);<br /> else<br /> fact = n * fact(n-1);<br /> return(fact); <br />}<br />Please note that there is no error handling added to this function <br />
  • 70.
  • 71. If a character string is to be received through the keyboard which function would work faster? <br />gets<br />scanf<br />
  • 72. gets<br />
  • 73. Consider the following piece of code and predict the output<br />100<br />4<br />2<br />99<br />void main() {<br /> int i=100;<br />clrscr();<br /> printf("%d", sizeof(sizeof(i)));<br /> getch(); <br />} <br />
  • 74. 2<br />
  • 75. Choose the correct output from the following code snippet.<br />It prints "God is Great“<br />Run time error<br />Compilation error<br />No output<br />void main() {<br /> static int a,b;<br /> if(a & b)<br /> printf("nGod is Great"); }<br />
  • 76. No Output<br />
  • 77. Which of the following header file is required for strcpy () function?<br />string.h<br />strings.h<br />stdio.h<br />file.h<br />
  • 78. string.h<br />
  • 79. Choose the correct answer<br />Main cannot be printed<br />%p is an invalid control string<br />Some address will be printed<br />Compile time error<br />main()<br />{<br /> Printf(“%p”,main);<br />}<br />
  • 80. Some address will be printed<br />
  • 81. What error will the below function give on compilation?<br />The function should be defined as int f(int a, int b)<br />Redeclaration of a<br />Missing parentheses in return<br />f(int a, int b)<br />{<br /> int a;<br /> a = 20;<br /> return a;<br />}<br />
  • 82. Redeclaration of a <br />
  • 83. Choose the correct output for the below code<br />123,201<br />121,200<br />120,201<br />120,199<br />main() <br />{<br /> int x=45,y=78;<br />clrscr();<br /> x = y-- + x--;<br /> y = ++y + ++x;<br /> printf ("%d %dn", x, y); <br />} <br />
  • 84. 123,201 <br />
  • 85. Consider the code snippet and choose the correct output<br />0<br />No Output<br />Null<br />1<br />void main() {<br /> static int i;<br /> i = i+1;<br /> printf("%d", i);<br /> getch(); <br />}<br />
  • 86. 1<br />
  • 87. What is the output of the below code?<br /> main(){ int a=10,*j;<br /> void *k; <br /> j=k=&a; j++; k++; printf("n %u %u ",j,k);<br />} <br />
  • 88. Compiler error: Cannot increment a void pointer<br />Explanation:<br />Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. <br />No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers <br />
  • 89. What is the output of the below code?<br />main()<br />{<br /> float i = 1.5;<br /> switch(i)<br /> {<br /> case 1: printf(“1”);<br /> case 2: printf(“2”);<br /> default: printf(“0”);<br />}}<br />
  • 90. Compiler Error: switch expression not integral<br />Explanation: <br />Switch statements can be applied only to integral types.<br />
  • 91. What is the output of the below code?<br />void main()<br />{ static int i;<br /> while(i<=10) (i>2)?i++:i--; printf(“%d”, i);<br />}<br />
  • 92. 32767<br />Explanation:<br />Since i is static it is initialized to 0. Inside the while loop the conditional operator evaluates to false, executing i--. <br />This continues till the integer value rotates to positive value (32767). The while condition becomes false and hence, comes out of the while loop, printing the i value. <br />
  • 93. What is the output of the below code?<br />main(){ char str1[] = ”some”;<br /> char str2[] =”some”;<br /> if (strcmp(str1,str2)) <br /> printf(“Strings are not equaln”);<br />}<br />
  • 94. No output <br />
  • 95. What is the output of the below code?<br />main(){ char p[ ]="%dn";<br /> p[1] = 'c';<br /> printf(p,65);}<br />
  • 96. A<br />Explanation:<br />Due to the assignment p[1] = ‘c’ the string becomes, “%cn”. <br />Since this string becomes the format string for printf and ASCII value of 65 is ‘A’, the same gets printed. <br />
  • 97. What is the output of the below code?<br />void main()<br />{ int i=5;<br /> printf(“%d”,i=++i ==6);<br />}<br />
  • 98. 1<br />Explanation:<br />The expression can be treated as i = (++i==6), because == is of higher precedence than = operator. <br />In the inner expression, ++i is equal to 6 yielding true (1). Hence the result.<br />
  • 99. What is the output of the following code?<br />main()<br />{<br /> int a[5] = {2,3};<br /> printf(" "n%d %d d"",a[2],a[3],a[4]);<br />}<br />
  • 100. 0 0 0 <br />
  • 101. What will be the output of the below program?<br />4<br />3<br />No output<br />0<br />#include<stdio.h><br />#define MIN(x,y)(x<y)?x:y<br />void main() {<br /> int x = 3, y = 4,z;<br /> z = MIN(x+y/2,y-1);<br /> if(z>0)<br /> printf("%d",z);<br />}<br />
  • 102. 3<br />
  • 103. Which of the following is correct about the statement given below?<br />structure engine is nested within structure maruti<br />structure maruti is nested within structure engine<br />structure maruti is nested within structure bolts<br />structure engine is nested within structure engine<br />maruti.engine.bolts = 25;<br />
  • 104. structure engine is nested within structure maruti<br />
  • 105. Which of the following is the correct output for the program given below?<br />1<br />2<br />5<br />4<br />int fun(int);<br />void main(){<br /> int i = 3;<br /> fun(i = fun(fun(i)));<br /> printf("%d",i);<br />}<br />fun(int i){<br /> i++;<br /> return(i);<br />}<br />
  • 106. 5 <br />
  • 107. Recursions works slower than loops<br />True<br />False<br />
  • 108. True<br />
  • 109. Choose the correct answer based on output from following piece of code<br />16, 25<br />9,49<br />Error in code<br />6,7<br />#define PRODUCT(x) (x*x)<br />main()<br />{<br /> int i = 3,j,k;<br /> j = PRODUCT (i++);<br /> k = PRODUCT(++i);<br /> printf(“n%d %d”, j,k);<br />}<br />
  • 110. 9,49 <br />
  • 111. Which of the following is the correct output for the given program?<br />d<br />Abcdefgh<br />e<br />Compile time error<br />void main()<br />{<br />printf("%c","abcdefgh"[4]);<br />}<br />
  • 112. e<br />
  • 113. Which of the following is the correct output for the program given below?<br />20<br />10<br />30<br />0<br />#include<stdio.h><br />void main() {<br /> union var {<br /> int a, b;<br /> };<br /> union var v;<br />v.a = 10;<br />v.b = 20;<br /> printf("%d",v.a); }<br />
  • 114. 20<br />
  • 115. On opening a file for reading which of the following activities are performed?<br />A pointer is set up which points to the first character in the file<br />The disk is searched for existence of the file<br />The file is brought into memory<br />
  • 116. a, b and c <br />
  • 117. The assignment operation X=Y means?<br />l-value of Y is stored in l-value of X<br />r-value of Y is stored in r- value of X<br />r-value of Y is stored in l-value of X<br />l-value of Y is stored in r-value of X<br />
  • 118. r-value of Y is stored in l-value of X <br />
  • 119. Which of the following statement is/are true?<br />A char data type variable always occupies one byte independent of the system architecture.<br />The sizeof operator is used to determine the amount of memory occupied by a variable.<br />Only i<br />Only ii<br />Both i & ii<br />Neither i nor ii<br />
  • 120. Only ii <br />
  • 121. Write down the equivalent pointer expression for referring element a[i][j][k][l] ? <br />
  • 122. a[i] == *(a+i)<br />a[i][j] == *(*(a+i)+j)<br />a[i][j][k] == *(*(*(a+i)+j)+k)<br />a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)<br />Hence Final output is *(*(*(*(a+i)+j)+k)+l) <br />
  • 123. The preprocessor can trap the simple errors like missing declarations, nested comments or mismatch of braces.<br />True<br />False<br />
  • 124. False<br />
  • 125. What is the output of the below code?<br />Syntax Error<br />No Output<br />Compiler Error<br />hello<br />#include main(){ <br />struct xx { <br /> int x=3;<br /> char name[]=”hello”;<br /> };<br />struct xx *s; <br />
  • 126. Compiler Error <br />
  • 127. What is the output of the below code?<br />H<br />Syntax error<br />Compiler error<br />E<br />void main()<br />{<br /> char *p;<br /> p=”Hello”;<br /> printf(“%cn”,*&*p);<br />}<br />
  • 128. H<br />
  • 129. In the following code<br />fp points to<br />A Structure which contains a char pointer which points to the first character in the file<br />The name of the file<br />The First character in the file<br />#include<stdio.h><br />main() {<br />FILE *fp;<br />fp=fopen("trail","r");<br />}<br />
  • 130. A Structure which contains a char pointer which points to the first character in the file<br />
  • 131. #include<stdio.h>main(){char line[80];scanf("%[^n]",line);printf("%s",line);}<br />what will scanf do ?<br />Compiler error<br />terminates reading input into variable line after newline<br />terminates reading input into variable line after entering ^n<br />
  • 132. Terminates reading input into variable line after newline <br />
  • 133. What is the output of the below code?<br />void main(){ char s[ ]="Welcome"; int i; for(i=0;s[ i ];i++) printf("n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);}<br />
  • 134. WWWW<br />eeee<br />llll<br />cccc<br />oooo<br />mmmm<br />eeee<br />
  • 135. Explanation:<br />s[i], *(i+s), *(s+i), i[s] are all different ways of expressing the same idea.<br />Generally array name is the base address for that array. <br />Here s is the base address. <br />i is the index number/displacement from the base address. <br />So, indirecting it with * is same as s[i]. i[s] may be surprising. But in the case of C it is same as s[i].<br />
  • 136. What does the following program do?<br />#include "math.h"<br />main(){<br /> int n,i,j,m;<br /> printf("Enter any number n");<br />scanf("%d", &n);<br /> for(i=1;i<=n;i++) {<br /> m=(int)(sqrt(i));<br /> for(j=1; j<=m;j++)<br /> if(func(i,j)!=1)<br /> break;<br />if(j==m+1)<br /> printf("%dt", i);<br /> }<br />}<br />int func(int u, int v){<br /> int min,i,val;<br /> min = (u<v?u:v);<br /> for(i=1;i<min;i++)<br /> if(u%i==0&&v%i==0)<br /> val = i;<br /> return(val);<br /> }<br />
  • 137. It will print the list of all primary numbers less than or equal to a given number n<br />
  • 138. You have given 1000 integers to sort. But, your RAM accommodates maximum of 100 integers only. How do your sort them?<br />
  • 139. We have to use external sorting (by tapes, disks etc.) mechanism. <br />We can use merge sort for internal sorting iterations. <br />
  • 140. How to allocate and deallocate memory for char** ? <br />
  • 141. In the order, first construct the main objects, and then construct the internal objects<br />Allocation<br />char** pp = new char*[10];<br /> for(int i=0;i<10;i++)<br /> char*[i]=new char[100];<br />
  • 142. In the reverse order, first destruct the internal objects, then destruct the main object<br />Deallocation<br /> for(int i=0;i<10;i++)<br /> delete [] char*[i]; <br /> delete[] pp;<br />
  • 143. What are the best data structures for insertion, deletion and search operations? <br />
  • 144. For Insertion and deletion - Linked list<br />For search - Map (it is a height balanced Red-black tree. We can search in O(log n))<br />
  • 145. Describe two scenarios where an application will crash due to stack overflow.<br />
  • 146. The two most common causes for a stack overflow is:<br />(i) An infinite recursion <br />int f1(){<br /> f2();<br />}<br />int f2() {<br /> f1(); <br />}<br />f1() calls f2(), which in turn calls f1() and so on. Eventually, the stack overflows.<br />
  • 147. (ii) An attempt to create a large array on the stack, for example:<br />int main()<br />{<br /> int a[100000000]; // array is too large <br /> int b =0; //b's address exceeds the stack's limits, error<br />}<br />If our program crashes due to a stack overflow, we need to check for infinite recursion or too large local objects<br />
  • 148. Can main function be overloaded? Explain.<br />
  • 149. No, only one type of calling is allowed in a program (with out arguments/with two arguments/ with three arguments)<br />
  • 150. How to count the number of set bits of an integer using that many number of iterations of for loop?<br />
  • 151. unsigned int v; // count the number of bits set in v<br />unsigned int c; // c accumulates the total bits set in v<br />for (c = 0; v; c++)<br />{<br /> v &= v - 1; // clear the least significant bit set<br />}<br />
  • 152. What is the output of this program?<br />#include <stdio.h><br />void main() { <br /> char s[20]; <br /> char *str = "SachinTendulker"; <br />sscanf(str, "%s", s); <br /> printf("%s", s); <br />sscanf(str+7, "%[duenTlk]", s); <br /> printf("%s", s); <br />sscanf(str+15, "%[^Ganguly]", s); <br /> printf("%sn", s); }<br />
  • 153. SachinTendulker<br />Explanation:<br />The above statement captures Sachin only into S, because of white space character.<br />The above statement captures Tendulker into S freshly. Because the format specifier has wildcard character [duenTlk], which means the scanning of string would be done as long as the character is one among the list of characters(starting from str+7).<br />when it encounters a new character which is not there in the list the reading stops.<br />sscanf(str, "%s", s);<br />sscanf(str+7, "%[duenTlk]", s);<br />
  • 154. The above statement captures r into S freshly. Because the format specifier has wildcard character [^Ganguly], which means the scanning of string would be done as long as the character is not one among the list of characters(starting from str+15).<br />when it encounters a new character which is there in the list(or when the null character occurs) the reading stops.<br />sscanf(str+15, "%[^Ganguly]", s);<br />
  • 155. What is the output of the following program?<br />main() {<br /> union abc {<br /> int a:1;<br /> int b:1;<br /> int c:1;<br />int d:1;<br /> int e:1;<br /> int f:1; <br /> int g:1;<br /> int h:1;<br /> };<br /> abc X;<br /> abc.a = abc.b = abc.c = abc.d = abc.e = abc.f =abc.g = abc.h = 1;<br />printf("%d",X);<br /> }<br />
  • 156. -1<br />Explanation:<br />By default, X is signed and has sign bit 1. Therefore, it is stored in 2’s complement form.<br />
  • 157. Consider the following code:<br />What is the scope difference between i and j?<br />int i;<br />static int j;<br />main( ) <br /> {<br /> }<br />
  • 158. Both i and j have global scope, but j cannot be accessed in the other modules(files) <br />
  • 159. How many times the following program would print India?<br />main( )<br />{<br /> printf(" n India");<br /> main();<br />}<br />
  • 160. Till stack does not overflow (Infinite loop)<br />
  • 161. What is heap? <br />
  • 162. The heap is where malloc(), calloc(), and realloc() get memory.<br />Getting memory from the heap is much slower than getting it from the stack. <br />On the other hand, the heap is much more flexible than the stack. Memory can be allocated at any time and deallocated in any order.<br />Such memory is not deallocated automatically; you have to call free().<br />Recursive data structures are almost always implemented with memory from the heap. <br />
  • 163. What would be the output of the following program if the array begins at the location 65472?<br />main( ) {<br /> int a[3][4] = {<br /> 1,2,3, 4,<br /> 4,3,2,1,<br /> 7,8,9,0<br /> };<br /> printf( "n %u %u ",a+1, &a+1);<br />}<br />
  • 164. 65488, 65520 <br />
  • 165. Provide simple implementation of int atoi(char* pStr)<br />
  • 166. int atoi(char* pStr) {<br /> int iRetVal=0;<br /> if (pStr){<br /> while (*pStr && *pStr<= ‘9’ && *pStr>=’0’)<br /> {<br />iRetval = (iRetval*10) + (*pStr – ‘0’);<br />pStr++;<br /> }<br /> }<br /> return iRetval;<br />} <br />
  • 167. What is the output of the below statement?<br />printf(“%d”); <br />
  • 168. Garbage Value. <br />Because, when we write printf("%d",x); this means compiler will print the value of x. <br />But as here, there is nothing after %d so garbage value is given as an output.<br />

×