This book is intended for anyone, who is interested in knowing about computers and basics of C. We are extremely happy to come out with this book on “Expertise your C” for students of all the streams in Computer Science. I have divided the syllabus into the small chapters so that the topics can be arranged properly. The topics within the chapters have been arranged in a proper sequence to ensure smooth flow of the subject. The book has been thoughtfully structured to serve as an ideal textbook for various courses offered in computer science.The book explains basic concepts like data types and control structures, decision control structure and loops, creating functions and using the standard C library. It also covers C preprocessor directives, handling strings, and error handling. It also discusses C programming under different environments like Windows and Linux. The book uses a lot of programming examples to help the reader gain a deeper understanding of the various C features. This book also aims to help prepare readers not just for the theoretical exams, but also the practical ones. It builds their C programming skills. It also helps in getting through job interviews.
Table of Contents
Chapter 1 Declaration Initializations
Chapter 2 Expressions
Chapter 3 Functions
Chapter 4 Pointers
Chapter 5 Strings
Chapter 6 Arrays
Chapter 7 Bitwise Operators
Chapter 8 Control Statements
Chapter 9 Pre-processors
Chapter 10 Floating Point Issues
Chapter 11 Structure and Union
Chapter 12 Input and Output
Preface
Dear Students,
This book is intended for anyone, who is interested in knowing about computers and basics of C++. We are extremely happy to come out with this book on “Expertise your C” for students of all the streams in BCA. I have divided the syllabus into the small chapters so that the topics can be arranged properly. The topics within the chapters have been arranged in a proper sequence to ensure smooth flow of the subject. The book has been thoughtfully structured to serve as an ideal textbook for various courses offered in computer science.
We thankful to great Almighty and especially to our parents for the encouragement and support that they have extended. We have made every possible effort to eliminate all the errors in this book. However if you find any, please let us know, that will improve us further.
Authors
Chapter 1 Declaration Initializations
Questions
1.1 What will be the output of the following program?
#include stdio.h
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
A. 2
B. 4
C. Vary compiler to compiler
D. Linker error
Answer: Option D
Explanation:
Linker Error : Undefined symbol 'i'
The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some other program and that address will be given to the current program at the time of linking. But linker finds that no other variable of name 'i' is available in any other program with memory space allocated for it. Hence a linker error has occurred.
1.2 What will be the output of the following program?
#include stdio.h
extern int i=3;
int main()
{
i = 20;
printf("%d\n",i);
return 0;
}
A. 20
B. 3
C. Vary compiler to compiler
D. Linker error
Answer: Option A
Explanation:
Extern variable will work when you take the variable outside the main function as shown in the example below.
1.3 What will be the output of the following program?
#include stdio.h
extern int i=3;
int main()
{
i = 20;
printf("%d\n",i);
void disp();
disp();
return 0;
}
void disp()
{
printf("%d",i);
}
A. 20 3
B. 3 20
C. 20
D. 3
Answer: Option B
Explanation:
First printf() will take the variable declared inside the main function and then it will call the function and gives the output as 3 20.
1.4 What will be the output of the following program?
#include stdio.h
int main()
{
char *s1;
char far *s2;
char huge *s3;
printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
return 0;
}
A. 2 ,4 ,6
B. 4, 4, 2
C. 2, 4, 4
D. 2, 2, 2
Answer: Option C
Any pointer size is 2 bytes.
So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.
1.5 What will be the output of the following program?
#include stdio.h
int main()
{
struct stu
{
char name[20];
float age;
int rn;
};
struct stu e = {"Rupinder"};
printf("%f, %d\n", e.age, e.rn);
return 0;
}
A. Garbage Value
B. Linker Error
C. 0.00000 , 0
D. 0 , 0
Answer: Option C
Explanation:
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
1.6 Variable names beginning with underscore is not encouraged. Why?
A. It is not standardized
B. To avoid conflicts since assemblers and loaders use such names
C. To avoid conflicts since library routines use such names
D. To avoid conflicts with environment variables of an operating system
Answer: C
Explanation: None.
1.7 All keywords in C are in
A. Lowercase letters
B. Uppercase letters
C. Camel Case letters
D. None
Answer: A
Explanation: None
1.8 What is the storage class of x in the following statement?
int x;
Answer: The storage class of x is auto if it is declared in main () and extern if it is declared outside the main ().
1.9 Which of the following special symbol is allowed in a variable name?
A. *
B.
C. -
D. _
Answer D
1.10 Which the correct output for the program given below?
#include stdio.h
int main()
{
int a[5]={3,4};
printf("%d%d%d\n",a[1],a[3],a[4]);
return 0;
}
A. Garbage Values
B. 4 0 0
C. 0 0 0
D. 3 4 0
Answer: B
When an automatic array is partially initialized, the remaining array elements are initialized to 0.
1.11 What is the difference between the following functions?
extern int get();
int get();
Answer: The first function tells you that this function is probably in another source file.
1.12 Which of the following can’t be a Keyword in C?
A. volatile
B. true
C. friend
D. export
Answer: A
Explanation: Volatile is a keyword in C.
1.13 What will happen if the below program is executed?
#include stdio.h
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
A. It will cause a compile time error
B. It will cause a run time error
C. 3
D. Infinte loop
Answer: C
1.14 Which of the following is not a valid variable name declaration?
A. float PI=3.14
B. double PI=3.14
C. int PI=3-14
D. #define PI 3.14
Answer: D
Explanation: # define PI 3.14 is a macro pre-processor, it is a textual substitution.
1.15 How would you round off a value from 1.44 to 2.0?
A. ceil(1.44)
B. floor(1.44)
C. roundup(1.44)
D. roundto(1.44)
Answer: A
Explanation:
#include stdio.h
#include math.h
int main()
{
printf("\n Result : %f" , ceil(1.44) );
printf("\n Result : %f" , ceil(2.32) );
printf("\n Result : %f" , floor(1.44) );
printf("\n Result : %f" , floor(2.32) );
return 0;
}
Output of the above program is
2.000000
3.000000
1.000000
2.000000
1.16 Comment on the output of this C code?
#include stdio.h
int main()
{
int variable = 12;
int Variable = 14;
printf("%d", variable);
return 0;
}
A. 12
B. 14
C. Syntax error
D. Garbage Value
Answer: A
Explanation: C is a Case sensitive language so it will automatically picks the value of the variable and displays it.
1.17 What is the output of the following program?
#include "stdio.h"
#include "conio.h"
void main()
{
printf("Hello");
}
A. Hello
B. printf() should have a prototype
C. Syntax error
D. Linker Error
Answer: A
Explanation: You can define your header files in double codes (“ ” ).
1.18 Give the output of the following code
#include stdio.h
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
A. The compiler will give an error
B. Program will compile and print 5 five times
C. Program will compile and print fail 5 times
D. Syntax error
Answer: C
Explanation: The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
1.19 What is the output of the following code
#include stdio.h
#include conio.h
void main()
{
float f=1.3;
if(f==1.3)
{
printf("Equal");
}
else
{
printf("Not equal");
}
getch();
}
A. Equal
B. Not Equal
C. Syntax Error
D. None of the above
Answer: B
Explanation: By default your 1.3 is double so the answer in not equal if you take double then A is the answer.
1.20 Give the output of the following code:
#include stdio.h
int main()
{
float f1 = 1.3;
if (f1 == 1.3f)
printf("equal\n");
else
printf("not equal\n");
}
A. Equal
B. Not Equal
C. Syntax Error
D. None of the above
Answer: A
Explanation: Now in this case your 1.3 is stored in floating point representation.
1.21 What is the output of the following program(32 bit machine)?
#include stdio.h
union Sti
{
int nu;
char m;
};
int main()
{
union Sti s;
printf("%d", sizeof(s));
return 0;
}
A. 2
B. 4
C. 3
D. 5
Answer: A
Explanation: Since the size of a union is the size of its maximum data type, here int is the largest hence 2. If the machine is of 64 bit type then your size will come out to be 4.
1.22 What is the output of the following code:
#include stdio.h
#include conio.h
void main()
{
float x=2,y=4;
float z;
clrscr();
z=x/y;
printf("%f",z);
getch();
}
A. 0.5
B. 0.500000
C. 0.50
D. Syntax error
Answer: B
1.23 Which of the following is true for variable names in C?
A. They can contain alphanumeric characters as well as special characters
B. It is not an error to declare a variable to be one of the keywords(like goto, static).
C. Variable names cannot start with a digit.
D. Variable can be of any length.
Answer: C
Explanation: According to C syntax you can’t start the variable name with a digit.
1.24 Which of the following is not a valid C variable name?
A. int number;
B. float rate;
C. int variable_count;
D. int $main;
Answer: D
Explanation: Since only underscore and no other special character is allowed in a variable name, it results in an error.
1.25 What is the output of the following code?
#include stdio.h
#include conio.h
void main()
{
int x=011;
printf("%d",x);
getch();
}
A. 9
B. 4
C. 3
D. 5
Answer: A
Explanation: 011 is the octal representation of 9
1.26 What values would be stored in variables x and y below?
int x;
float y;
Answer: If they are defined in main function then they would be treated as automatic storage class variables. In this case both x and y will be having garbage values.
1.27 By default a real number is treated as a
A. float
B. double
C. long double
D. long int
Answer: Option B
1.28 Which of the following is not user defined data type?
1. struct
2. long int
3. enum
4. Arrays
Answer: Option B
Explanation:
C data types classification are
1. Primary data types
1. int
2. char
3. float
4. double
5. void
2. Secondary data types (or) User-defined data type
1. Array
2. Pointer
3. Structure
4. Union
5. Enum
1.29 Give the output of the following code:
#include stdio.h
int main()
{
int if=2,of=3;
printf("%d%d",if,of);
}
A. Syntax error
B. 2 3
C. 2 2
D. Linker error
Answer: A
Explanation: You can’t take keywords as Identifiers.
1.30 Give the output of the following:
#include stdio.h
int main()
{
display();
return 0;
}
void display()
{
printf("Enhancing C Skills");
}
A. No error
B. display() doesn’t get invoked.
C. display() is called before it is defined.
D. None of the above
Answer: C
Explanation: In this program the compiler will not know that the function display () exists. So, the compiler will generate "Type mismatch in re declaration of function display ()".
1.31 Which keyword is used to prevent any changes in the variable within a C program?
A. volatile
B. static
C. mutable
D. const
Answer: D
Explanation: const is a keyword constant in C program.
1.32 A variable declared in a function body can be used in main ()?
A. True
B. False
C. True if it is declared as const
D. True if it is declared as static
Answer: False
Explanation: Since the scope of the variable declared within a function is restricted only within that function.
1.33 Give the output of the following code:
#include stdio.h
int main()
{
printf("Programming\rSkills\n");
return 0;
}
A. ProgrammingSkills
B. Skillsprogramming
C. Skillsmming
D. mmingSkills
Answer: C
Explanation: r is carriage return and moves the cursor back. Progra is replaced by Skills
1.34 Suppose a is an integer variable which is given a value 32767. If we increment value of a by 1 then what will be the output?
#include stdio.h
#include conio.h
void main()
{
int a=32767;
clrscr();
a++;
printf("%d",a);
getch();
}
A. 32768
B. -32768
C. Garbage Value
D. 0
Answer: B
1.35 Unsigned Integer occupies how many bytes?
A. 1
B. 2
C. 3
D. 4
Answer: D
1.36 Give the output of the following code:
#include stdio.h
#include conio.h
void main()
{
printf("%d",printf("Hello"));
getch();
}
A. Hello5
B. Hello
C. Syntax error
D. 15
Answer: A
Explanation: printf statement will print Hello, “Hello” due to inner printf statement and 5 as length due to outer printf statement.
1.37 Output of the following program fragment is
x = 5;
y = x++;
printf(“%d%d”, x, y);
A. 5,6
B. 5,5
C. 6,6
D. 6,5
Answer: A
Explanation: x is incremented by 1 and before that increment the value is assigned to y so value of x=6 and y=5.
1.38 The output of the following is
main()
{
int a = 75;
printf (“%d%%”, a);
}
A. 75%
B. 75
C. 75%%
D. None of the above
Answer : A
1.39 Is it true that a function may have several declarations and only one definition?[true/false]
Answer: true
1.40 Which amongst the following is not a keyword?
A. int
B. float
C. char
D. external
Answer: D
1.41 What will you do to treat the constant 2.45 as long double?
A. Use 2.45LD
B. Use 2.45L
C. Use 2.45DL
D. Use 2.45LF
Answer: B
Explanation:
Given 2.45 is a double constant.
To specify 2.45 as long double, we have to add L to the 2.45. (i.e. 2.45L)
1.42 What is the output of this C code?
#include stdio.h
int main()
{
int i=10,j=2;’
printf(“%d”printf(%d%d“,i,j));
}
A. Compile time error
B. 10 2 5
C. 10 2 2
D. 10 10 2
Answer: B
1.43 The number of digits present after decimal in float is__________?
A. 1
B. 2
C. 5
D. 6
Answer: D
1.44 What is the size of float in 32-bit compiler?
A. 1
B. 2
C. 4
D. 6
Answer: C
1.45 What is the output of this C Code?
#include stdio.h
int main()
{
float a=2.34555555;
printf("%f",a);
}
A. 2.345556
B. 2.345555555
C. 2.345555
D. None of the above
Answer: A
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 2
Expressions
2.1 Give the output of the following code.
#include stdio.h
int main()
{
int i=-3, j=2, k=0, m;
m = ++i ++j ++k;
printf("%d, %d, %d, %d", i, j, k, m);
return 0;
}
a) -2 3 1 1
b) 2 3 2 1
c) 1 2 3 2
d) 2 2 1 3
Answer: a
2.2 Are the following statements same?
1.
a = 20 ? b = 30: c = 30;
2.
(a =20) ? b : c = 30;
A. Yes
B. No
Answer: No
2.3 Which of the following correctly shows the hierarchy of arithmetic operations in C?
a) / + * -
b) * - / +
c) / * + -
d) / - + *
Answer: C
2.4 Which one of the following is not a Logical Operator?
A.
B.
C. ||
D. !
Answer: A
Explanation:
Bitwise operators:
is a Bitwise AND operator.
Logical operators:
is a Logical AND operator.
|| is a Logical OR operator.
! is a NOT operator.
So, ' ' is not a Logical operator.
2.5 Give the output of the following
void main()
{
int i=10;
i = !i 14;
printf("i=%d", i);
}
a) 10
b) 14
c) 0
d) 1
Answer: c
2.6 Which of the following is the correct order of evaluation for the below expression?
z = x + y * z / 4 % 2 – 1
a) * / % + - =
b) = * / % + -
c) / * % - + =
d) * % / - + =
Answer: a
2.7 Which of the following is the correct order if calling functions in the below code?
a = f1(23, 14) * f2(12/4) + f3();
a) f1,f2,f3
b) f3,f2,f1
c) Depends on Compiler
d) None of above.
Answer: C
2.8 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 Lvalue required
D. Garbage Value
Answer: C
Explanation:
Variable b is not assigned.
It should be like:
b = a = 5 ? 100 : 200;
2.9 The modulus operator cannot be used with long double.
A. True
B. False
Answer: Option A
Explanation: fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point or long double divisions.
2.10 In which order do the following gets evaluated
1. Relational
2. Arithmetic
3. Logical
4. Assignment
a) 2134
b) 1234
c) 4321
d) 3214
Answer: a
2.11 Give the output of the following code
#include stdio.h
void main()
{
double ch;
printf("enter a value btw 1 to 2:");
scanf("%lf", ch);
switch (ch)
{
case 1:
printf("1");
break;
case 2:
printf("2");
break;
}
}
A. Compile time error
B. 1
C. 2
D. Varies
Answer: A
Explanation: Switch declaration should be of integral type.
2.12 Which is correct with respect to size of the data types?
a) char int floatAbbildung in dieser Leseprobe nicht enthalten
b) int char floatAbbildung in dieser Leseprobe nicht enthalten
c) char int doubleAbbildung in dieser Leseprobe nicht enthalten
d) double char int
Answer: c
2.13 What will be the output of the following program?
void main()
{
int a, b, c, d;
a = 3;
b = 5;
c = a, b;
d = (a, b);
printf("c=%d d=%d", c, d);
}
a) c=3 d=3
b) c=3 d=5
c) c=3 d=4
d) c=4 d=3
Answer: b
2.14 Which of the following comments about the ++ operator are correct?
a) It is a unary operator
b) The operand can come before or after the operator
c) It associates from the right
d) All of above
Answer: d
2.15 Give the output of the following
void main()
{
int a=10, b=20;
char x=1, y=0;
if(a,b,x,y){
printf("EXAM");
}
}
a) XAM is printed
b) Exam is printed
c) Compile time error
d) Nothing
Answer: d
2.16 Give the output of the following
int b=15, c=5, d=8, e=8, a;
a = b c ? c d ? 12 : d e ? 13 : 14 : 15;
printf ( " %d ", a ) ;
a) 13
b) 14
c) 15
d) 12
Answer: b
2.17 What will be the output of the program?
#include stdio.h
int main()
{
int k, num=30;
k = (num 5 ? (num =10 ? 100 : 200): 500);
printf("%d", num);
return 0;
}
a) 200
b) 30
c) 100
d) 500
Answer: b
2.18 What is the output of the following Code?
void main()
{
printf("%x",-1 4);
}
a) fff0
b) fff1
c) fff2
d) Nothing
Answer: a
2.19 The modulus operator cannot be used with a long double.
A. True
B. False
Answer: Option A
Explanation:
fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point or long double divisions
2.20 Can we use a switch statement to switch on strings?
A. Yes
B. No
Answer: A
2.21 What is the output of the following Program?
#include stdio.h
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
A. Even
B. Odd
C. Default
D. Nothing
Answer: B
2.22 What is the output of the following?
#include stdio.h
int i;
int main()
{
if (i);
else
printf("Ëlse");
return 0;
}
A. if block is executed
B. else block is executed
C. Unprediticable
D. Error
Answer : B
2.23 What is the output of the following?
#include stdio.h
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
}
A. 1
B. 0
C. Run time Error
D. Compile time Error
Answer: Run time error
2.24 What will be the output of the following code?
void main()
{
int y=10;
if(y++ 9 y++!=10 y++ 11)
printf("%d", y);
else
printf("%d", y);
}
a) 11
b) 12
c) 13
d) 14
Answer: c
2.25 What will be the output of the following code?
#include stdio.h
void main()
{
int y=10;
if(y++ 9 y++!=11 y++ 11)
printf("%d", y);
else
printf("%d", y);
}
a) 11
b) 12
c) 13
d) 14
Answer: b
2.26 Two different operators would always have different Associativity.
a) Yes
b) No
Answer: b
2.27 Which of the following is the correct usage of conditional operators used in C?
a) a b ? c=30 : c=40;
b) a b ? c=30;
c) max = a b ? a c?a:c:b c?b:c
d) return (a b)?(a:b)
Answer: c
2.28 Will the expression *p = p be disallowed by the compiler?
a) Yes
b) No
Answer: b
2.29 What will be the output of the program?
#include stdio.h
int main()
{
int x=12, y=7, z;
z = x!=4 || y == 2;
printf("z=%d", z);
return 0;
}
a) z=0
b) z=1
c) z=2
d) z=3
Answer: b
2.30 What will be the output of the program?
#include stdio.h
int main()
{
static int a[20];
int i = 0;
a[i] = i ;
printf("%d, %d, %d", a[0], a[1], i);
return 0;
}
a) 1 0 1
b) 1 1 1
c) 0 0 0
d) 0 1 0
Answer: c
2.30 What will be the output of the program?
#include stdio.h
int main()
{
int x=4, y, z;
y = --x;
z = x--;
printf("%d, %d, %d", x, y, z);
return 0;
}
a) 4 3 3
b) 4 3 2
c) 2 3 3
d) 3 2 2
Answer: c
2.31 What will be the output of the following code?
#include stdio.h
int main()
{
int x=55;
printf("%d, %d, %d", x =55, x=40, x =10);
return 0;
}
a) 1 40 1
b) 1 55 1
c) 1 53 0
d) 1 1 1
Answer: a
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 3
Functions
3.1 Give the output of the following code.
#include stdio.h
void main()
{
cgc();
void cgc()
{
printf("Programming");
}
}
a) Programming
b) Compile time error
c) Nothing
d) Varies
Answer : b
3.2 Can we return more than one value to the function?
a) True
b) False
Answer: a
3.3 The value obtained in the function is given back to main by using ________ keyword?
a) return
b) static
c) new
d) volatile
Answer: a
3.4 . Can we use a function as a parameter of another function? [ Eg: void wow(int func()) ].
a) Yes, and we can use the function value conveniently
b) Yes, but we call the function again to get the value, not as convenient as in using variable
c) No, C does not support it
d) This case is compiler dependent
Answer: c
3.5 Can functions returns structure in c?
a) true
b) false
c) Depends on the compiler
d) Depends on the standard
Answer: a
3.6 Can functions returns enumeration constants in c?
a) true
b) false
c) depends on the compiler
d) depends on the standard
Answer: a
3.7 What will be the output of the following program?
#include stdio.h
void fun(int*, int*);
int main()
{
int i=5, j=2;
fun(i, j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *i, int *j)
{
*i = *i**i;
*j = *j**j;
}
a) 5,2
b) 4,6
c) 2,5
d) 25,4
Answer: d
Explanation:
Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.
Step 2: fun(i, j); Here the function fun() is called with two parameters i and j (The denotes call by reference. So the address of the variable i and j are passed. )
Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.
Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.
Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.
Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.
Step 7: printf("%d, %d", i, j); It prints the value of variable i and j.
3.8 What is the output of the following program?
#include stdio.h
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
a) Print 5,4,3,2,1
b) Print 1,2,3,4,5
c) Print 5,4,3,2,1,0
d) Infinite loop.
Answer: d
Explanation:
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);.
The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5) infinitely.
Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1. Because before calling the function, it decrements the value of 'n'.
3.9
How many times the program will print "Programming”?
int main()
{
printf("Programming");
main();
return 0;
}
a) Infinite times
b) 32767 times
c) 0 times
d) Till stack overflow
Answer: d
Explanation:
A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.
3.10 What will happen after running the following code:
main()
{
printf("%p", main);
}
a) Error
b) Will make an infinite loop
c) Garbage value
d) None of above
Answer: c
3.11 Any C program must contains
a) At least one function
b) Need not contain any function
c) Needs input data
d) None of the above
Answer: a
3.12 Determine output:
main()
{
int i = abc(10);
printf("%d", --i);
}
int abc(int i)
{
return(i++);
}
a) 10
b) 9
c) 13
d) None of these
Answer: b
3.13 Determine output:
main()
{
int a=1, b=10;
swap(a,b);
printf("\n%d%d", a,b);
}
swap(int x, int y)
{
int temp;
temp=x;
x=y;
a) 1 1
b) 1 10
c) 10 1
d) None of these
Answer: b
3.14 The keyword used to transfer control from a function back to the calling function is
a) Switch
b) goto
c) break
d) return
Answer: d
3.15 Find out the error in this code
#include stdio.h
int main()
{
int f(int);
int b;
b = f(20);
printf("%d", b);
return 0;
}
int f(int a)
{
a 20? return(10): return(20);
}
a) Prototype declaration
b) No error
c) return statement can be used with conditional operator
d) None of above.
Answer: C
3.16 What will be the output of the following
#include stdio.h
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
a) Prints 5,4,3,2,1
b) Prints 1,2,3,4,5
c) Prints 5,4,3,2,1,0
d) Infinite loop
Answer: d
3.17 There is a error in the below program. Which statement will you add to remove it?
#include stdio.h
int main()
{
int a;
a = f(10, 3.14);
printf("%d", a);
return 0;
}
float f(int aa, float bb)
{
return ((float)aa + bb);
}
a) Add prototype: float f(aa, bb)
b) Add prototype: float f(int, float)
c) Add prototype: float f(float, int)
d) Add prototype: float f(bb, aa)
Answer: b
3.18 Every Function must return a value?
a) Yes
b) No
Answer: b
3.19 Find out the error in the program?
#include stdio.h
int main()
{
int a=10;
void f();
a = f();
printf("%d", a);
return 0;
}
void f()
{
printf("Hi");
}
a) Not allowed assignment
b) Doesn’t print anything
c) No Error
d) None of above
Answer: a
3.20 What will be the output of the program?
#include stdio.h
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
int a=3;
fun(a);
return 0;
}
void fun(int n)
{
if(n 0)
{
fun(--n);
printf("%d,", n);
fun(--n);
}
}
a) 0 2 1 0
b) 1 1 2 0
c) 0 1 0 2
d) 0 1 2 0
Answer: d
3.21 What will be the output of the program?
#include stdio.h
int main()
{
int fun(int);
int i = fun(10);
printf("%d", --i);
return 0;
}
int fun(int i)
{
return (i++);
}
a) 9
b) 10
c) 11
d) 8
Answer: a
3.22 What will be the output of the program?
#include stdio.h
int check (int, int);
int main()
{
int c;
c = check(10, 20);
printf("c=%d", c);
return 0;
}
int check(int i, int j)
{
int *p, *q;
p=i;
q=j;
i =45 ? return(*p): return(*q);
}
a) 10
b) 20
c) 30
d) Compile time error
Answer: d
3.23 What is the output of the following code?
main ()
{
int a = 4;
change { a };
printf ("%d", a);
}
change (a)
int a;
{
printf("%d", ++a);
}
outputs
a) 55
b) 45
c) 54
d) 44
Answer: C
Explanation :
change (a) , prints 5 but the value of 'a' in main ( ) is still 4. So main( ) will print 4.
3.24 What is the output of the following code?
main()
{
int abc();
abc ();
( *abc) ();
}
int abc( )
{
printf ("come");
}
a) prints come come
b) Compile time error
c) Run time error
d) come
Answer: a
Explanation :
The function abc can be invoked as abc( ) or ( *abc ) ( ). Both are two different ways of doing the same thing.
3.25 It is not advisable to use macros instead of functions because
a) it increases the code size
b) no type checking will be done
c) recursion is not possible
d) All of above
Answer: a
3.26 What is the output of the following code?
main()
{
inc(); inc(); inc();
}
inc()
{
static int x;
printf("%d", ++x);
}
a) prints 012
b) prints 123
c) prints 3 consecutive, but unpredictable numbers
d) prints 111
Option: b
Explanation :
By default x will be initialized to 0. Since its storage class is static, it presents its exit value ( and forbids re initialization on re-entry ). So, 123 will be printed.
3.27 What is the output of the following code?
void fun(int*, int*);
int main()
{
int i=5, j=2;
fun(i, j);
printf("%d, %d", i, j);
return 0;
}
void fun(int *i, int *j)
{
*i = *i**i;
*j = *j**j;
}
a) 5 2
b) 10 4
c) 2 5
d) 25 4
Answer: d
Explanation:
Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.
Step 2: fun(i, j); Here the function fun() is called with two parameters i and j (The denotes call by reference. So the address of the variable i and j are passed. )
Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.
Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.
Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.
Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.
Step 7: printf("%d, %d", i, j); It prints the value of variable i and j.
Hence the output is 25, 4.
3.28 What will be the output of the program?
int reverse(int);
int main()
{
int no=5;
reverse(no);
return 0;
}
int reverse(int no)
{
if(no == 0)
return 0;
else
printf("%d,", no);
reverse (no--);
}
a) Print 5, 4, 3, 2, 1
b) Print 1, 2, 3, 4, 5
c) Print 5, 4, 3, 2, 1, 0
d) Infinite loop
Answer: d
Explanation:
Step 1: int no=5; The variable no is declared as integer type and initialized to 5.
Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.
The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);.
The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5) infinitely.
Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1. Because before calling the function, it decrements the value of 'n'.
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 4
Pointers
4.1 What will be the output of the following program?
# include stdio.h
void fun(int x)
{
x = 30;
}
int main()
{
int y = 20;
fun(y);
printf("%d", y);
return 0;
}
a) 30
b) 20
c) Compile time error
d) Run time error
Answer: b
4.2 A Pointer is
a) A Keyword used to create variables
b) A variable that stores address of an instruction
c) A variable that stores address of other variable
d) All of above.
Answer: c
4.3 The operator used to get value at address stored in a pointer variable is
a) *
b)
c)
d) ||
Answer: a
4.4 Choose the best answer. Prior to using a pointer variable
a) It should be declared
b) It should be initialized
c) It should be both declared and initialized
d) None of these
Answer: c
4.5 What will be the output?
main()
{
char *p;
p = "Hello";
printf("%cn",**p);
}
a) Hello
b) H
c) Ello
d) o
Answer: c
4.6 What will be the output of the following code?
#include stdio.h
void main()
{
int *ptr, a=10;
ptr = a;
*ptr += 1;
printf("%d, %d", *ptr, a);
}
a) 10,10
b) 10,11
c) 11,10
d) 11,11
Answer: d
4.7 What will be the output of the following program code?
#include stdio.h
void main()
{
int i = 10;
void *p = i;
printf("%fn", *(float *)p);
}
a) Error
b) 10
c) 0.000000
d) None of these
Answer: c
4.8 The statement int **p;
a) Illegal
b) Is illegal but meaningless
c) is syntactically and semantically correct
d) None of these
Answer: c
4.9 What will be the output of the following program code?
#include stdio.h
void main()
{
int i=3, *j, **k;
j = i;
k = j;
printf("%d%d%d", *j, **k, *(*k));
}
a) 444
b) 111
c) 333
d) 433
e) Garbage Value
Answer: C
4.10 Which of the following statements are true after execution of the program.
void main()
{
int a[10], i, *p;
a[0] = 1;
a[1] = 2;
p = a;
(*p)++;
}
a) a[1]=3
b) a[0]=3
c) a[0]=2
d) a[1]=2
Answer: C
4.11 What is the output of this code?
#include stdio.h
void main()
{
int k = 5;
int *p = k;
int **m = p;
printf("%d%d%d\n", k, *p, **p);
}
a) 5 5 5
b) 5 5 4
c) Compile time error
d) 5 junk junk
Answer: c
4.12 What is the output of the following code?
int main()
{
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = arr[0];
float *ptr2 = ptr1 + 3;
printf("%f ", *ptr2);
printf("%d", ptr2 - ptr1);
return 0;
}
a) 90.500000
3
b) 90.500000
12
c) 10.000000
12
d) 0.500000
3
Answer: a
Explanation: When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type.
4.13 The reason for using pointers in a C program is
a) Pointers allow different functions to share and modify their local variables.
b) To pass large structures so that complete copy of the structure can be avoided
c) Pointers enable complex “linked” data structures like linked lists and binary trees.
d) All of the above
Answer: (D)
Explanation: See below explanation
(A) With pointers, address of variables can be passed different functions can use this address to access the variables.
(B) When large structure variables passed or returned, they are copied as everything is passed and returned by value in C. This can be costly with structure containing large data. To avoid this copying of large variables, we generally use poitner for large structures so that only address is copied.
(C) With pointers, we can implement “linked” data structures. Java uses reference variables to implement these data structures. Note that C doesn’t support reference variables.
4.14 What is the output of the following code?
#include stdio.h
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
++*p;
p += 2;
printf("%d", *p);
return 0;
}
a) 2
b) 3
c) 4
d) Compile Error
Answer: (B)
Explanation: The expression ++*p is evaluated as “++(*p)” . So it increments the value of first element of array (doesn’t change the pointer p).
When p += 2 is done, p is changed to point to third element of array.
4.15 What is the output of this C code?
int main()
{
int i = 10;
void *p = i;
printf("%d\n", (int)*p);
return 0;
}
a) Compile time error
b) 10
c) 11
d) None of above
Answer: a
4.16 What is the output of this C code?
int *f();
int main()
{
int *p = f();
printf("%d\n", *p);
}
int *f()
{
int j = 10;
return j;
}
a) Compile time error
b) 10
c) Address of p
d) None of above
Answer: a
4.17 What is the output of this C code?
int main()
{
int i = 97, *p = i;
foo(p);
printf("%d ", *p);
return 0;
}
void foo(int **p)
{
int j = 2;
*p = j;
printf("%d ", **p);
}
a) 2 2
b) 2 97
c) Undefined Behaviour
d) Run time error
Answer: a
4.18 What is the output of this C code?
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
int *r = p;
printf("%d", (**r));
}
a) 1
b) Compile time error
c) Address of a
d) Garbage Value
Answer: a
4.19 Comment on the output of this C code?
int main()
{
int a = 10;
int **c -= a;
}
a) You cannot apply any arithmetic operand to a pointer.
b) We don’t have address of an address operator
c) Both (a) and (b)
d) None of the above
Answer: b
4.20 What is meaning of following declaration?
int(*ptr[5])();
a) ptr is pointer to function.
b) ptr is array of pointer to function.
c) ptr is pointer to array of function.
d) None of these
Answer: b
4.21 What is meaning of following pointer declaration?
int(*(*ptr1)())[2];
a) ptr is pointer to function.
b) ptr is pointer to such function which return type is pointer to an array.
c) ptr is array of pointer to function.
d) ptr is pointer array of function.
Answer: b
4.22 What is size of generic pointer in c?
a) 0
b) 1
c) 2
d) 3
Answer: c
Explanation:
Size of any type of pointer is 2 byte (In case of near pointer)
Note. By default all pointers are near pointer if default memory model is small.
4.23 What will be output of following c code?
#include stdio.h
int main(){
int *p1,**p2;
double *q1,**q2;
clrscr();
printf("%d %d ",sizeof(p1),sizeof(p2));
printf("%d %d",sizeof(q1),sizeof(q2));
getch();
return 0;
}
a) 2 2 2 2
b) 2 3 4 5
c) 1 2 3 4
d) 2 4 6 8
Answer: a
Explanation : Size of any type of pointer is 2 byte (In case of near pointer)
4.24 Are the expression *ptr++ and ++*ptr are same?
a) True
b) False
Answer: b
4.25 What will be the output of the program ?
int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}
a) ink
b) ack
c) ite
d) let
Answer: ink
4.26 Point out the error in the program
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j 5; j++)
{
printf("%d", a);
a++;
}
return 0;
}
a) Error: Declaration syntax
b) Error: Expression syntax
c) Error: LValue required
d) Error: Rvalue required
Answer: c
4.27 What will be the output of the program ?
int main()
{
int x=30, *y, *z;
y=x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d", x, y, z);
return 0;
}
a) x=31, y=502, z=502
b) x=31, y=500, z=500
c) x=31, y=498, z=498
d) x=31, y=504, z=504
Answer: d
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 5 Strings
5.1 What will be the output of the following program?
#include stdio.h
#include string.h
int main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s", strcpy(str2, strcat(str1, str2)));
return 0;
}
a) Hello
b) Hello World
c) World Hello
d) World
Answer: b
5.2 Which of the following statements are correct about the program below?
#include stdio.h
int main()
{
char str[20], *s;
printf("Enter a string");
scanf("%s", str);
s=str;
while(*s != '')
{
if(*s = 97 *s = 122)
*s = *s-32;
s++;
}
printf("%s",str);
return 0;
}
a) The code converts a string into integer
b) The code converts lower case character to upper case
c) The code converts upper case character to lower case
d) Error
Answer: b
5.3 The library function used to reverse the string is
a) strcat()
b) strrev()
c) stringrev()
d) stringreve()
Answer: b
5.4 If the two string are identical strcmp() function returns
a) 0
b) 1
c) -1
d) True
Answer: a
5.5 What will be the output of the program?
#include stdio.h
int main()
{
printf(5+"Good Morning");
return 0;
}
a) Good Morning
b) Good
c) Morning
d) M
Answer: c
5.6 Which of the following function is more appropriate for reading in a multi-word string?
a) printf()
b) scanf()
c) gets()
d) puts()
Answer: c
5.7 What will be the output of the program?
#include stdio.h
void main()
{
printf("Hello", "Chandigarh");
}
a) Hello
b) Hello Chandigarh
c) Chandigarh
d) Error
Answer: a
5.8 What will be the output of the following program?
#include stdio.h
int main()
{
char *names[] = { "Ramu", "Inder", "Hally", "Jaypee", "Poras"};
int i;
char *t;
t = names[3];
names[3] = names[4];
names[4] = t;
for(i=0; i =4; i++)
printf("%s,", names[i]);
return 0;
}
a) Ramu Inder Hally Jaypee Poras
b) Ramu Hally Inder Jaypee Poras
c) Poras Inder Hally Jaypee Ramu
d) Ramu Inder Hally Poras Jaypee
Answer: d
5.9 What will be the output of the program?
#include stdio.h
#include conio.h
int main()
{
static char s[50] = "Chandigarh Group of Colleges";
int i=0;
char ch;
clrscr();
ch = s[++i];
printf("%c", ch);
ch = s[i++];
printf("%c", ch);
ch = i++[s];
printf("%c", ch);
return 0;
}
a) hha
b) hah
c) had
d) ahh
Answer: a
5.10 What is the output of the following code?
#include stdio.h
int main()
{
char str[] = "Chandigarhs";
printf("%d", sizeof(str));
return 0;
}
a) 11
b) 10
c) 9
d) 0
Answer: a
5.11 What will be the output of the following code?
#include stdio.h
int main()
{
char str = "CGC";
printf("%s", str);
return 0;
}
a) Error
b) CGC
c) cgc
d) Address of str
Answer: a
5.12 What will be the output of the following code?
#include stdio.h
int main()
{
char str[] = "Punjab";
str[0]='K';
printf("%s, ", str);
str = "Haryana";
printf("%s", str+1);
return 0;
}
a) Kunjab Haryana
b) Punjab aryana
c) Error
d) Kunjab Karyana
Answer: c
5.13 What will be the output of the following code?
#include stdio.h
int main()
{
printf(5+"College");
return 0;
}
a) College
b) Error
c) ge
d) ege
Answer: ge
5.14 What will be the output of the following code?
#include stdio.h
#include string.h
int main()
{
char *str1 = "CGC";
char *str2 = "Landran";
char *str3;
str3 = strcat(str1, str2);
printf("%s %s", str3, str1);
return 0;
}
a) CGC Landran
b) CGC Landran CGC Landran
c) Landran CGC Landran CGC
d) CGC CGC Landran Landran
Answer: b
5.15 What will be the output of the following code?
int main()
{
char p[] = "%d\n";
p[1] = 'c';
printf(p, 65);
return 0;
}
a) A
b) a
c) c
d) 65
Answer: a
Explanation:
Step 1 : char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d".
Step 2 : p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
Step 3 : printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
5.16 What will be the output of the following?
int main()
{
printf("%d\n", strlen("123456"));
return 0;
}
a) 6
b) 5
c) 4
d) None of above
Answer: Option A
Explanation:
The function strlen returns the number of characters in the given string.
Therefore, strlen("123456") returns 6.
Hence the output of the program is "6".
5.17 What will be the output of the program?
int main()
{
char sentence[80];
int i;
printf("Enter a line of text\n");
gets(sentence);
for(i=strlen(sentence)-1; i =0; i--)
putchar(sentence[i]);
return 0;
}
a) The sentence will get printed in same order as it entered
b) The sentence will get printed in reverse order
c) Half of the sentence will get printed
d) None of above
Answer: b
5.18 If S is an array of 80 characters, then the value assigned to S through the statement scanf("%s",S) with input 1 2 3 4 5 would be
a) “12345”
b) Nothing since 12345 is an integer
c) S is an illegal name for string
d) Compile time error
Answer: a
5.19 Under which of the following conditions, the size of an one-dimensional array need not be specified?
a) when initialization is a part of definition
b) when it is a declaration
c) when it is a formal parameter and an actual argument
d) All of the above
Answer: d
5.20 How will you print \n on the screen?
a) printf(“\n”);
b) echo \\n ;
c) printf(‘\n’);
d) printf(“\\n”);
Answer: d
5.21 Strcat function adds null character
a) Only if there is space
b) Always
c) Depends on the code
d) Depends on the compiler
Answer: b
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 6 Arrays
6.1 What will be the output of the following code?
int main()
{
int a[5] = {5, 1, 15, 20, 25};
int i, j, m;
i = ++a[1];
j = a[1]++;
m = a[i++];
printf("%d, %d, %d", i, j, m);
return 0;
}
a) 3 2 27
b) 1 5 27
c) 2 3 25
d) 1 2 25
Answer: a
Explanation: Initially i will be incremented from 1 to 2 and then j will be incremented the value of a[1] to 3 but as it is post increment so the value allocated to j is still 2.m will increment the ith location from 2 to 4 so the values printed 3 2 27.
6.2 Which of the following statements mentioning the name of the array begins DOES NOT yield the base address?
a) When array name is used with the sizeof operator.
b) When array name is operand of the operator.
c) When array name is passed to scanf() function.
d) When array name is passed to printf() function.
a) A
b) B
c) A,B
d) B,A
Answer: c
6.3 An array elements are always stored in ________ memory locations.
a) Sequential
b) Random
c) Sequential and Random
d) None of the above
Answer: a
6.4 What is the output of the following programming?
int main()
{
int i;
int arr[5] = {2,3};
for (i = 0; i 5; i++)
printf("%d ", arr[i]);
return 0;
}
a) 2,3,0,0,0
b) After 2 ,3 three garbage values
c) 2,3,3,3,3
d) None of the above
Answer: a
6.5 What will be the output of the following code?
int a[10]={1,2,3,4,5,6,7,8,9,10};
*p=a;
printf(“%d %d”,p[7],[p[a[7]]);
a) 7 7
b) 7 8
c) 8 9
d) 9 9
Answer: c
6.6 What is the output of the following code?
main( )
{
int a[4]={2,3};
printf(“%d”,a[3]);
}
a) 0
b) Garbage Value
c) Syntax error
d) None of above
Answer: a
6.7 In C Programming, If we need to store word "INDIA" then syntax is as below –
a) char name[6] = {'I','N','D','I','A','\0'}
b) char name[6] = {"I","N","D","I","A"}
c) char name[6] = {'I','N','D','I','A'}
d) char name[];
name = "INDIA"
Answer: a
6.8 What is the output of the following code?
#include stdio.h
int main()
{
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i 2; i++)
for (j = 0; j 2; j++)
printf("%d ", a[i][j]);
return 0;
}
a) 1 2 3 4
b) Compile time error
c) 4 garbage values
d) 4 3 2 1
Answer: b
Explanation: There is compilation error in the declaration " int a[][] = {{1,2},{3,4}};". Except the first dimension, every other dimension must be specified. int arr[] = {5, 6, 7, 8} //valid int arr[][5] = {}; //valid int arr[][] = {}; //invalid int arr[][10][5] = {}; //valid int arr[][][5] = {}; //invalid
6.9 What will be the output of the following code?
void main()
{
int a[10];
printf("%d %d", a[-1], a[12]);
}
a) 0 0
b) Garbage Value 0
c) 0 garbage Value
d) 2 garbage values
Answer: d
Explanation: In c compiler does not check array with its bounds, value at the computed location is displayed.
6.10 Which of the following statements are correct about the program below?
#include stdio.h
void main()
{
int size, i;
scanf("%d", size);
int arr[size];
for(i=1; i =size; i++)
{
scanf("%d", arr[i]);
printf("%d", arr[i]);
}
}
a) The code is erroneous since the statement declaring array is invalid.
b) The code is erroneous since the subscript for array used in for loop is in the range 1 to size.
c) The code is correct and runs successfully.
d) The code is erroneous since the values of array are getting scanned through the loop.
Answer: a
Explanation: The statement int arr[size]; produces an error, because we cannot initialize the size of array dynamically. Constant expression is required here.
Example: int arr[10];
One more point is there, that is, usually declaration is not allowed after calling any function in a current block of code. In the given program the declaration int arr[10]; is placed after a function call scanf().
6.11 What will be the output of the following code?
#include stdio.h
int main(void)
{
char p;
char ar[10] = {1, 2, 3, 4, 5, 6, 9, 8};
p = (ar + 1)[5];
printf("%d", p);
return 0;
}
a) 5
b) 6
c) 9
d) Error
Answer : c
Explanation : x[i] is equivalent to *(x + i), so (ar + 1)[5] is *(ar+ 1 + 5), or ar[6].
6.12 What will be the output of the program?
#include stdio.h
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = a[0][0];
fun(ptr);
return 0;
}
void fun(int **p)
{
printf("%d", **p);
}
a) 1
b) 2
c) 3
d) 4
Answer: a
6.13 What will be the output of the following code?
int main()
{
static int arr[] = {0, 1, 2, 3, 4};
int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
int **ptr=p;
ptr++;
printf("%d, %d, %d", ptr-p, *ptr-arr, **ptr);
*ptr++;
printf("%d, %d, %d", ptr-p, *ptr-arr, **ptr);
*++ptr;
printf("%d, %d, %d", ptr-p, *ptr-arr, **ptr);
++*ptr;
printf("%d, %d, %d", ptr-p, *ptr-arr, **ptr);
return 0;
}
a) 0, 0, 0
1, 1, 1
2, 2, 2
3, 3, 3
b) 1, 1, 2
2, 2, 3
3, 3, 4
4, 4, 1
c) 1, 1, 1
2, 2, 2
3, 3, 3
3, 4, 4
d) 0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
Answer: c
6. 14 What will be the output of the program ?
#include conio.h
#include stdio.h
void main()
{
int ar[1]={10};
printf("%d",0[ar]);
getch();
}
a) 10
b) 0
c) Error
d) Garbage Value
Answer: a
6.15 What is the output of the following code?
#include conio.h
#include stdio.h
void main()
{
int ar[1]={10,20,30};
printf("%d",5[ar]);
getch();
}
a) Too many intializers
b) 0
c) Garbage Value
d) None of the above
Answer: a
6.16 What will be the out of the following code?
#include conio.h
#include stdio.h
void main()
{
int ar[5]={10,20,30};
printf("%d",5[ar]);
getch();
}
a) 0
b) Garbage Value
c) Less values initialized
d) None of the above
Answer: b
6.17 What will be the output of the following?
#include stdio.h
int main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf("%d", sizeof(arr)/sizeof(arr[0]));
return 0;
}
a) 4
b) 16
c) 2
d) 0
Answer: a
6.18 What will be the output of the program if the array begins 1200 in memory?
#include stdio.h
int main()
{
int arr[]={2, 3, 4, 1, 6};
printf("%u, %u, %u", arr, arr[0], arr);
return 0;
}
a) 1200, 1202, 1204
b) 1200, 1200, 1200
c) 1200, 1204, 1208
d) 1200, 1202, 1200
Answer: b
6.19 What will be the output of the program if the array begins at address 65486?
#include stdio.h
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%u, %u", arr, arr);
return 0;
}
a) 65486, 65488
b) 65486, 65486
c) 65486, 65490
d) 65486, 65487
Answer: b
6.20 What is the output of the following code?
#include stdio.h
int main()
{
int arr[] = {12, 14, 15, 23, 45};
printf("%f, %f", arr, arr);
return 0;
}
a) 65486, 65488
b) Abnormal Termination of the program
c) 12 14
d) 65486, 65487
Answer: b
6.21 What is the output of this C code?
#include stdio.h
void f(int a[][])
{
a[0][1] = 3;
int i = 0, j = 0;
for (i = 0;i 2; i++)
for (j = 0;j 3; j++)
printf("%d", a[i][j]);
}
void main()
{
int a[2][3] = {0};
f(a);
}
a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
Answer: c
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 7 Bitwise Operators
7.1 Assuming, integer is 2 byte, what will be the output of the program?
#include stdio.h
int main()
{
printf("%x", -1 1);
return 0;
}
a) ffff
b) 0fff
c) 0000
d) fff0
Answer: a
7.2 Which of the following statements are correct about the program?
#include stdio.h
int main()
{
unsigned int num;
int i;
scanf("%u", num);
for(i=0; i 16; i++)
{
printf("%d", (num i 1 15)?1:0);
}
return 0;
}
a) It prints all even bits from num
b) It prints all odd bits from num
c) It prints binary equivalent num
d) Error
Answer: c
7.3 Which operator operates operations on data in binary level?
a) Logical Operator
b) Relational Operator
c) Conditional Operator
d) Bitwise Operator
Answer: d
7.4 What is the output of this C code?
#include stdio.h
int main()
{
unsigned int a = 10;
a = ~a;
printf("%d\n", a);
}
a) -9
b) -10
c) -11
d) 10
Answer: c
7.5 What is the output of this C code?
#include stdio.h
int main()
{
int a = 2;
if (a 1)
printf("%d\n", a);
}
a) 0
b) 1
c) 2
d) No Output.
Answer: c
7.6 What is the output of this C code?
#include stdio.h
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}
a) 6 -6 0 0
b) 6 -5 0 1
c) -6 -6 0 1
d) 6 -6 0 1
Answer: d
7.7 What is the output of this C code?
#include stdio.h
void main()
{
int a = -5;
int k = (a++, ++a);
printf("%d\n", k);
}
a) -3
b) -5
c) 4
d) Undefined
Answer: a
7.8 What is the output of this C code?
#include stdio.h
int main()
{
int x = 2;
x = x 1;
printf("%d\n", x);
}
a) 4
b) 1
c) Depends on the compiler
d) Depends on the Endianness of the machine
Answer: a
7.9 Bitwise can be used in conjunction with ~ operator to turn off 1 or more bits in a number.
a) Yes
b) No
Answer: a
7.10 Which of the following statements are correct about the program?
#include stdio.h
int main()
{
unsigned int num;
int i;
scanf("%u", num);
for(i=0; i 16; i++)
{
printf("%d", (num i 1 15)?1:0);
}
return 0;
}
a) It prints all even bits from num
b) It prints all odd bits from num
c) It prints binary equivalent num
d) Error
Answer: c
7.11 Left shifting a number by 1 is always equivalent to multiplying it by 2.
a) True
b) False
Answer: True
7.12 Bitwise can be used to reverse a sign of a number.
a) Yes
b) No
Answer: No
7.13 Which of the following statements are correct about the program?
#include stdio.h
int main()
{
unsigned int num;
int c= 0 ;
scanf( "%u" , num);
for (;num;num = 1 )
{
if (num 1 )
c++;
}
printf( "%d" , c);
return 0 ;
}
a) It counts the number of bits that are ON (1) in the number num.
b) It counts the number of bits that are OFF (0) in the number num.
c) It sets all bits in the number num to 1
d) Error
Answer: a
7.14 Which of the following statements are correct about the program?
#include stdio.h
char *fun(unsigned int num, int base);
int main()
{
char *s;
s=fun(128, 2);
s=fun(128, 16);
printf("%s",s);
return 0;
}
char *fun(unsigned int num, int base)
{
static char buff[33];
char *ptr = buff[sizeof(buff)-1];
*ptr = '';
do
{
*--ptr = "0123456789abcdef"[num %base];
num /=base;
}while(num!=0);
return ptr;
}
a) It converts a number to a given base.
b) It converts a number to its equivalent binary.
c) It converts a number to its equivalent hexadecimal.
d) It converts a number to its equivalent octal.
Answer: a
7.15 Bitwise and | are unary operators
a) True
b) False
Answer: b
7.16 What will be the output of the program?
#include stdio.h
int main()
{
char c=48;
int i, mask=01;
for(i=1; i =5; i++)
{
printf("%c", c|mask);
mask = mask 1;
}
return 0;
}
a) 12400
b) 12480
c) 12500
d) 12556
Answer: b
7.17 What will be the output of the program?
#include stdio.h
int main()
{
printf("%d %d", 32 1, 32 0);
printf("%d %d", 32 -1, 32 -0);
printf("%d %d", 32 1, 32 0);
printf("%d %d", 32 -1, 32 -0);
return 0;
}
a) Garbage values
b) 64 32
0 32
16 32
0 32
c) All zeros
d) 8 0
0 0
32 0
0 16
Answer: b
7.18 What will be the output of the program?
#include stdio.h
int main()
{
unsigned int res;
res = (64 (2+1-2)) (~(1 2));
printf("%d", res);
return 0;
}
a) 32
b) 64
c) 0
d) 128
Answer: a
7.19 What will be the output of the program ?
#include stdio.h
int main()
{
int i=4, j=8;
printf("%d, %d, %d", i|jj|i, i|jj|i, i^j);
return 0;
}
a) 4, 8, 0
b) 1, 2, 1
c) 12, 1, 12
d) 0, 0, 0
Answer: c
7.20 On left shifting, the bits from the left are rotated and brought to the right and accommodated where there is empty space on the right?
a) True
b) False
Answer: b
7.21 Predict the output of following program.
#include stdio.h
int main()
{
int a=10;
int b=2;
int c;
c=(a b);
printf("c= %d",c);
return 0;
}
a) c= 12
b) c= 10
c) c= 2
d) c= 0
Answer: c
Explanation : Bitwise AND () operator copies bit(s), if they are exist both of the operands. Here, binary of a is "1010" and binary of b is "0010". Thus, result of expression (a b) is "0010" which is equivalent to 2 in Decimal.
7.22 Predict the output of following program.
#include stdio.h
#define MOBILE 0x01
#define LAPPY 0x02
int main()
{
unsigned char item=0x00;
item |=MOBILE;
item |=LAPPY;
printf("I have purchased ...:");
if (item MOBILE){
printf("Mobile, ");
}
if (item LAPPY){
printf("Lappy");
}
return 1;
}
a) I have purchased ...:
b) I have purchased ...:Mobile, Lappy
c) I have purchased ...:Mobile,
d) I have purchased ...:Lappy
Answer: b
I have purchased ...:Mobile, Lappy
Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is "0001" and binary of Macro LAPPY (0x02) is "0010", then result of the expression item |=MOBILE; will be "0001" and second expression item |=LAPPY; will return "0011". Thus, both conditions (item MOBILE) and (item LAPPY) will be true.
7.23 Consider the given statement:
int y = 10 ^ 2
What will be the value of y?
a) 5
b) 6
c) 7
d) 8
Answer: Correct answer: d
XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:
a b (a^b)
_______________________
0 0 0
0 1 1
1 0 1
1 1 0
Here, binary of 10 is "1010" and binary of 2 is "0010", then the result of statement (10 ^ 2) will be "1000", which is equivalent to 8 in decimal.
7.24 Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?
a) Bitwise AND ()
b) Bitwise OR (|)
c) Bitwise XOR (^)
d) Bitwise NOT (~)
Answer: a
Explanation Bitwise AND () Operator can be used to check whether a number if EVEN or ODD, consider the statement (num 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.
7.25 What is the output of the following code?
#include stdio.h
int main()
{
int count;
for (count=1; count =10; count+=1)
if (count 1)
printf("%2d is ODD number\n",count);
else
printf("%2d is EVEN number\n",count);
return 0;
}
Output:
1 is ODD number
2 is EVEN number
3 is ODD number
4 is EVEN number
5 is ODD number
6 is EVEN number
7 is ODD number
8 is EVEN number
9 is ODD number
10 is EVEN number
7.26 Left shift ( ) and Right shift ( ) operators are equivalent to _____________ by 2.
a) Multiplication and Division
b) Division and Multiplication
c) Multiplication and Remainder
d) Remainder and Multiplication
Answer: a
Explanation:
#include stdio.h
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d",num);
printf("Multiplication by 2 = %d\n", (num 1));
printf("Division by 2 = %d\n",(num 1));
return 0;
}
Enter an integer number: 100
Multiplication by 2 = 200
Division by 2 = 50
7.27 Bitwise | can be used to set a bit in number.
a) Yes
b) No
Answer: Yes
7.28 What is the output of this C code?
int main()
{
int a = 10, b = 5, c = 3;
b != !a;
c = !!a;
printf("%d\t%d", b, c);
}
a) 5 1
b) 0 3
c) 5 3
d) 1 1
Answer: a
7.29 What will be the output of the program ?
#include stdio.h
int main()
{
int i=4, j=8;
printf("%d, %d, %d\n", i|jj|i, i|jj|i, i^j);
return 0;
}
a) 2 6 8
b) 4 7 8
c) 0 3 6
d) 12 1 12
Answer : d
7.30 What will be the output of the program?
#include stdio.h
int main()
{
printf("%d %d %d %d\n", 4 1, 8 1);
return 0;
}
a) 4 1 7 4
b) 4 6 1
c) 2 4 Garbage Value Garbage Value
d) 2 4
Answer: c
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 8 Control Statements
8.1 What is the output of given program if user enter value 99?
#include stdio.h
void main()
{
int i;
printf("Enter a number:");
scanf("%d", i); // 99 is given as input.
if(i%5 == 0){
printf("nNumber entered is divisible by 5");
}
}
a) Enter a number : 99
b) Number entered is divisible by 5
c) Run time error
d) Compile time error
Answer: a
8.2 How many times CGC will be printed?
#include stdio.h
int main()
{
int i = 1024;
for (; i; i = 1)
printf("CGC");
return 0;
}
a) 10
b) 11
c) Infinite loop
d) Compile time error
Answer: b
Explanation:
In for loop, mentioning expression is optional. = is a composite operator. It shifts the binary representation of the value by 1 to the right and assigns the resulting value to the same variable. The for loop is executed until value of variable i doesn't drop to 0.
8.3 What is the output of given program if user enter "xyz" ?
#include stdio.h
void main()
{
float age, AgeInSeconds;
printf("Enter your age:");
scanf("%f", age);
AgeInSeconds = 365 * 24 * 60 * 60 * age;
printf("You have lived for %f seconds", AgeInSeconds);
}
a) Enter your age: xyz you have lived for 0 seconds
b) Enter your age: xyz you have lived for 0.00000 seconds
c) Enter your age: xyz “after that program will stop”
d) Enter your age: error
Answer: b
8.4 How many types CGC will be printed?
#include stdio.h
#define PRINT(i, limit) do \
{ \
if (i++ limit) \
{ \
printf("CGC\n"); \
continue; \
} \
}while(0)
a) 1
b) CGC
c) 4
d) Compile time error
Answer: b
8.5 What will be the output of the following code?
#include stdio.h
int main()
{
int i = 0;
switch (i)
{
case '0': printf("Chandigarh");
break;
case '1': printf("Group");
break;
default: printf("of Colleges");
}
return 0;
}
a) Chandigarh
b) Group
c) ChandigarhGroup
d) of Colleges
Answer: d
8.6 Predict the output of the below program:
#include stdio.h
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
a) Even
b) Odd
c) Default
d) Error
Answer: b
Explanation: The expression i 1 returns 1 if the rightmost bit is set and returns 0 if the rightmost bit is not set. As all odd integers have their rightmost bit set, the control goes to the block labeled ODD.
8.7 What will be the output of the following?
#include stdio.h
void main()
{
int i=10;
printf("i=%d", i);
{
int i=20;
printf("i=%d", i);
i++;
printf("i=%d", i);
}
printf("i=%d", i);
}
a) 10 10 11 11
b) 10 20 21 21
c) 10 20 21 10
d) 10 20 21 20
Answer: c
8.8
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 time
b) 255 times
c) 256 times
d) 254 times
Answer: Option B
Explanation:
The while(j = 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.
8.9 What is the output of this C code?
int main()
{
int a = 0, i = 0, b;
for (i = 0;i 5; i++)
{
a++;
continue;
}
}
a) 1
b) 2
c) 4
d) 3
Answer: c
8.10 What is the output of this C code?
void main()
{
int i = 0;
int j = 0;
for (i = 0;i 5; i++)
{
for (j = 0;j 4; j++)
{
if (i 1)
continue;
printf("Hi \n");
}
}
}
a) Hi is printed 9 times
b) Hi is printed 7 times
c) Hi is printed 6 times
d) Hi is printed 8 times
Answer: 8
8.11 What is the output of this C code?
void main()
{
int i = 0;
if (i == 0)
{
printf("Hello");
continue;
}
}
a) Hello is printed 1 time
b) Hello is printed 20 times
c) Compile time error
d) None of above
Answer: c
8.12 What is the output of the below program?
#include stdio.h
int main()
{
int i = 0;
switch (i)
{
case '0': printf("Chandigarh");
break;
case '1': printf("Group");
break;
default: printf("CGC");
}
return 0;
}
a) Chandigarh
b) Group
c) CGC
d) None of these
Answer: CGC
8.13 Predict the output of the below program:
#include stdio.h
#define EVEN 0
#define ODD 1
int main()
{
int i = 3;
switch (i 1)
{
case EVEN: printf("Even");
break;
case ODD: printf("Odd");
break;
default: printf("Default");
}
return 0;
}
a) Even
b) Odd
c) Default
d) Compile Time error
Answer: Odd
8.14 What is the output of the following code?
void main()
{
double k = 0;
for (k = 0.0; k 3.0; k++)
printf("CGC");
}
a) Run time error
b) Compile time error
c) CGC will be printed thrice
d) CGC will be printed twice
Answer: c
8.15 What will be the value of i and j after execution of following program?
#include stdio.h
void main()
{
int i, j;
for(i=0,j=0;i 10,j 20;i++,j++){
printf("i=%d %t j=%d", i, j);
}
}
a) 10 10
b) 10 20
c) 20 20
d) Run time error
Answer: 20 20
8.16 What will be the output of the given program?
#include stdio.h
void main()
{
int a=11,b=5;
if(a=5) b++;
printf("%d %d", ++a, b++);
}
a) 11 7
b) 12 8
c) 6 6
d) 6 7
Answer: Option C
Solution:
Here if condition evaluates to true as a non-zero value i.e 5 is assigned to a.
So the value of a = 5 and after increment value of b = 6.
In printf statement due to pre-increment of a value of a printed will be 6 and due to post-increment of b value of b printed will be 6 and not 7.
8.17 What will be the output of the given program?
#include stdio.h
void main()
{
int value=0;
if(value)
printf("well done ");
printf("CGC");
}
a) Well done CGC
b) CGC
c) Compiler error
d) None of these
Answer: Option B
Solution:
As the value of variable value is zero so, it evaluates to false in the if condition.
8.18 What will be the output of given program?
#include stdio.h
void main()
{
int a=3;
for(;a;printf("%d ", a--);
}
a) No Output
b) 3 2 1 0
c) 3 2 1
d) Infinite Loop
Answer: Option C
Solution:
Decrement operator in for loop statement are executed until the condition is true.
So it is executed till "a" not equal to zero and printf statement inside for loop print a value of "a"
8.19 What will be the output of given program?
#include stdio.h
void main()
{
int i=1, j=-1;
if((printf("%d", i)) (printf("%d", j)))
printf("%d", i);
else
printf("%d", j);
}
a) 1 -1 1
b) 1 -1 -1
c) 1
d) -1
Answer: a
Solution: Here if statement is executed since we know that printf() function return the number of character print.
Here printf("%d", i) return 2 because it print 1 and newline i.e 2.
And, printf("%d', j) return 3 because it print -1 and newline i.e number of character is 3.
Therefore if statement look like if(2 3) yes its true.
So if statement will execute. And answer is 1 -1 1.
8.20 What will be the output of the following piece of code?
for(i = 0; i 100; i++);
printf("%d", i);
a) 100
b) 101
c) 0 to 100
d) Compile time error
Answer: a
Solution:
Due to semicolon(;) at the end of the for loop, after 100 iterations for loop will be terminated and the result will be 100.
8.21 What is the output of this C code?
int main()
{
int i = 0;
for (; ; ;)
printf("In for loop\n");
printf("After loop\n");
}
a) Compile time error
b) Infinite loop
c) After loop
d) In for loop
Answer: Compile time error
8.22 Which keyword can be used for coming out of recursion?
a) break
b) return
c) exit
d) Both a and b
Answer: b
8.23 What is the output of this C code?
int main()
{
printf("before continue ");
continue;
printf("after continue\n");
}
a) before continue
b) after continue
c) both a and b
d) Compile time error
Answer: d
8.24 The output of the code below is?
void main()
{
int i = 0;
if (i == 0)
{
goto label;
}
label: printf("Hello");
}
a) Nothing
b) Error
c) Infinite Hello
d) Hello
Answer: d
8.25 What will be the following code's output if choice = 'R'?
switch(choice)
{
case 'R' : printf("Chandigarh");
case 'W' : printf("Group");
case 'B' : printf("of");
default : printf("Colleges");break;
}
a) Chandigarh
b) Group
c) Of
d) Chandigarh Group of Colleges
Answer: d
8.26 What will be printed if the following code is executed?
void main()
{
int x=0;
for( ; ; )
{
if( x++ == 4 ) break;
continue;
}
printf("x=%d", x);
}
a) x=0
b) x=4
c) x=5
d) Compile time error
Answer: x=5
8.27 Consider the following code:
void main()
{
int a[5] = {6,8,3,9,0}, i=0;
if(i != 0)
{
printf("%d", a[i]);
}
else
printf("%d", a[i++]);
}
a) 8
b) 3
c) 9
d) 6
Answer: d
8.28 The keyword ‘break’ cannot be simply used within:
a) do-while
b) if-else
c) while
d) for
Answer: b
8.29 What is the output of this C code?
int main()
{
int i = 0;
char c = 'a';
while (i 2){
i++;
switch (c) {
case 'a':
printf("%c ", c);
break;
break;
}
}
printf("after loop\n");
}
a) a after loop
b) a a after loop
c) after loop
d) None of the above
Answer: b
8.30 What is the output of this C code?
int main()
{
printf("%d ", 1);
goto l1;
printf("%d ", 2);
l1:goto l2;
printf("%d ", 3);
l2:printf("%d ", 4);
}
a) 1 4
b) 2 3
c) 1 2
d) 4 4
Answer: a
8.31 What is the output of this C code?
int main()
{
printf("%d ", 1);
l1:l2:
printf("%d ", 2);
printf("%d\n", 3);
}
a) Compile time error
b) 1 2 3
c) 1 2
d) 1 3
Answer: b
8.32 What will be the output of the following code?
void main()
{
int x = 5;
if (x 1);
printf("Hello");
}
a) Nothing
b) Run time error
c) Hello
d) Varies
Answer: c
8.33 Which of the following statements are correct about the below C-program?
#include stdio.h
int main()
{
int x = 10, y = 100%90, i;
for(i=1; i 10; i++)
if(x != y);
printf("x = %d y = %d", 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) 4
d) 2,4
Answer: b
8.34 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", i);
if(i 2)
goto here;
}
return 0;
}
void fun()
{
here:
printf("It works");
}
a) No error
b) It works
c) fun() can’t be accessed
d) goto cannot takeover control to other function
Answer: d
8.35 What will be the output of the program?
#include stdio.h
int main()
{
float a = 0.7;
if(0.7 a)
printf("Hi");
else
printf("Hello");
return 0;
}
a) Hi
b) Hello
c) Hi Hello
d) Error
Answer: a
8.36 What is the output of the following code?
#include stdio.h
int main()
{
int a = 10, b;
a =5 ? b=100: b=200;
printf("%d", b);
return 0;
}
a) 100
b) 200
c) L value required
d) Garbage Value
Answer: C
8.37 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", a, b, c);
return 0;
}
a) 0 1 3
b) 1 3 4
c) 3 1 3
d) 0 2 4
Answer: c
8.38 What will be the output of the program?
#include stdio.h
int main()
{
int x = 10, y = 20;
if(!(!x) x)
printf("x = %d", x);
else
printf("y = %d", y);
return 0;
}
a) y=20
b) x=10
c) x=0
d) x=1
Answer: b
8.39 What will be the output of the program?
#include stdio.h
int main()
{
int i=4;
switch(i)
{
default:
printf("This is default");
case 1:
printf("This is case 1");
break;
case 2:
printf("This is case 2");
break;
case 3:
printf("This is case 3");
}
return 0;
}
a) This is default
This is case 1
b) This is default
This is case 2
c) This is case 2
d) This is default
Answer: a
8.40 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", x, y, z);
return 0;
}
a) x=2, y=1, z=1
b) x=2, y=3, z=1
c) x=2, y=10 z=2
d) x=2, y=2, z=2
Answer: a
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 9 Preprocessor
9.1 What will be the output of the program?
#include stdio.h
#define MAX(a, b, c) (a b ? a c ? a : c: b c ? b : c)
int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d", x);
return 0;
}
a) 5
b) 7
c) 9
d) 10
Answer: d
9.2 A preprocessor directive is a message from compiler to a linker.
a) True
b) False
Answer: b
9.3 What will be the output of the program?
#include stdio.h
#define PRINT(i) printf("%d,",i)
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
a) 2 3 4
b) 2 4 5
c) 3 3 5
d) 3 4 5
Answer: a
9.4 What will be the output of the program?
#include stdio.h
#define CGC college
int main()
{
printf("CGC");
return 0;
}
a) College
b) CGC
c) Error
d) Nothing
Answer: CGC
9.5 What will be the output of the program?
#include stdio.h
#define MIN(x, y) (x y)? x : y;
int main()
{
int x=3, y=4, z;
z = MIN(x+y/2, y-1);
if(z 0)
printf("%d", z);
return 0;
}
a) 3
b) 4
c) 0
d) No Output
Answer: 3
9.6 What will the CGC macro in the following program be expanded to on preprocessing? will the code compile?
#include stdio.h
#define CGC(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
CGC(x, y, int);
printf("%d %d\n", x, y);
return 0;
}
a) It Compiles
b) Compiles with a warning
c) Not Compile
d) None of above
Answer: c
Explanation:
The code won't compile since declaration of t cannot occur within parenthesis.
9.7 What will be the output of the program?
#include stdio.h
#define CUBE(x) (x*x*x)
int main()
{
int a, b=3;
a = CUBE(b++);
printf("%d, %d\n", a, b);
return 0;
}
a) 9 4
b) 27 4
c) 27 6
d) None of above
Answer: c
Explanation:
The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.)
Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3.
Step 2: a = CUBE(b++); becomes
= a = b++ * b++ * b++;
= a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement.
= a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6)
Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b.
Hence the output of the program is 27, 6.
9.8 What will be the output of the following code?
#include stdio.h
#define FUN(i, j) i##j
int main()
{
int va1=10;
int va12=20;
printf("%d\n", FUN(va1, 2));
return 0;
}
a) 10
b) 20
c) 1020
d) 12
Answer: b
9.9 Every C program will contain at least one preprocessor directive.
a) Yes
b) No
Answer: b
9.10 A preprocessor command
a) need not start on a new line
b) need not start on the first column
c) has # as the first character
d) comes before the first executable statement
Answer: c
9.11 What will be the output of the program?
#include stdio.h
#define int char
void main()
{
int i = 65;
printf("sizeof(i)=%d", sizeof(i));
}
a) sizeof(i)=2
b) sizeof(i)=1
c) Compiler Error
d) None of these
Answer: b
9.12 What will be the output of the program code?
#include stdio.h
#define a 10
void main()
{
#define a 50
printf("%d", a);
}
a) 50
b) 10
c) Compile time error
d) Multiple declaration of a
9.13 What will be the output of the program code?
#include stdio.h
#define clrscr() 100
void main()
{
clrscr();
printf("%dn", clrscr());
}
a) 0
b) 1
c) 100
d) Error
Answer: 100
9.14 What will be the output of the following program?
#include stdio.h
#define prod(a,b) a*b
void main()
{
int x=3,y=4;
printf("%d", prod(x+2,y-1));
}
a) 15
b) 12
c) 10
d) 11
Answer: c
9.15 What will be output if you will compile and execute the following c code?
#include stdio.h
#define max 5
void main(){
int i = 0;
i = max++;
printf("%d", i++);
}
a) Compile time Error
b) 5
c) 6
d) 7
Answer: a
9.16 C preprocessors can have compiler specific features.
a) True
b) False
c) Depends upon the standard
d) Depends on the platform
Answer: a
9.17 What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include stdio.h
#define SWAP(a, b, c)(c t; t=a, a=b, b=t)
int main()
{
int x=10, y=20;
SWAP(x, y, int);
printf("%d %d", x, y);
return 0;
}
a) It compiles
b) Not Compiles
c) Compiles but print nothing
d) Run time error
Answer: Not Compile
9.18 What will be output if you will compile and execute the following c code?
#include
#define x 5+2
int main(){
int i;
i=x*x*x;
printf("%d",i);
return 0;
}
a) 27
b) 343
c) 222
d) Error
Answer: a
Explanation : As we know #define is token pasting preprocessor it only paste the value of micro constant in the program before the actual compilation start. If you will see intermediate file you will find:
test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the 5+2 in place of x in program. So, i=5+2*5+2*5+2
=5+10+10+2
=27
9.19 #include is called?
a) Preprocessor Directives
b) Inclusion Directive
c) Macro
d) None of the above
Answer: a
9.20 C preprocessor is the first step during compilation?
a) Yes
b) No
c) Depends on the compiler
d) None
Answer: Yes
9.21 Will the program compile successfully?
#include stdio.h
int main()
{
#ifdef NOTE
int a;
a=10;
#else
int a;
a=20;
#endif
printf("%d", a);
return 0;
}
a) Yes
b) No
Answer: Yes
9.22 What will be the output of the program?
#include stdio.h
#define MIN(x, y) (x y)? x : y;
int main()
{
int x=3, y=4, z;
z = MIN(x+y/2, y-1);
if(z 0)
printf("%d", z);
return 0;
}
a) 3
b) 4
c) 0
d) No Output
Answer: a
9.23 What will be the output of the program?
#include stdio.h
#define PRINT(i) printf("%d,",i)
int main()
{
int x=2, y=3, z=4;
PRINT(x);
PRINT(y);
PRINT(z);
return 0;
}
a) 2 3 4
b) 2 4 6
c) 2 2 2
d) 4 4 4
Answer: a
9.24 What will be the output of the program?
#include stdio.h
#define MAX(a, b, c) (a b ? a c ? a : c: b c ? b : c)
int main()
{
int x;
x = MAX(3+2, 2+7, 3+7);
printf("%d", x);
return 0;
}
a) 5
b) 9
c) 10
d) 2+ 7
Answer: c
9.25 #include statement must be written.
a) Before main function
b) Before getch
c) Before any scanf/printf
d) It can be written anywhere
Answer: c
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 10 Floating Point Issues
10.1 What will you do to treat the constant 3.14 as a long double?
a) use 3.14LD
b) use 3.14L
c) use 3.14DL
d) use 3.14LF
Answer: Option B
Explanation:
Given 3.14 is a double constant.
To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)
10.2 Which of the following range is a valid long double (Turbo C in 16 bit DOS OS) ?
a) 3.4E-4932 to 1.1E+4932
b) 3.4E-4932 to 3.4E+4932
c) 1.1E-4932 to 1.1E+4932
d) 1.7E-4932 to 1.7E+4932
Answer: Option A
Explanation:
The range of long double is 3.4E-4932 to 1.1E+4932
10.3 Which statement will you add in the following program to work it correctly?
#include stdio.h
int main()
{
printf("%f\n", log(36.0));
return 0;
}
a) #include conio.h
b) #include math.h
c) #include iostream.h
d) #include dos.h
Answer: b
Explanation:
math.h is a header file in the standard library of C programming language designed for basic mathematical operations.
10.4 We want to round off x, a float, to an int value, The correct way to do is
a) y = (int)(x + 0.5)
b) y = int(x + 0.5)
c) y = (int)x + 0.5
d) y = (int)((int)x + 0.5)
Answer: Option A
Explanation:
Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.
y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)
10.5 What will be the output of the program?
int main()
{
float fval=7.29;
printf("%d", (int)fval);
return 0;
}
a) 0
b) 0.0
c) 7
d) 7.0
Answer: c
10.6 What will be the output of the program?
#include stdio.h
#include math.h
int main()
{
printf("%f", sqrt(36.0));
return 0;
}
a) 6
b) 6.0
c) 6.000000
d) Error
Answer: c
10.7 What will be the output of the program?
#include stdio.h
int main()
{
float f=43.20;
printf("%e, ", f);
printf("%f, ", f);
printf("%g", f);
return 0;
}
a) 4.320000e+01, 43.200001, 43.2
b) 4.3, 43.22, 43.21
c) 4.3e, 43.20f, 43.00
d) Compile time error
10.8 What will be the output of the following code?
#include stdio.h
int main()
{
float a=0.9;
if(a 0.9f)
printf("College");
else
printf("CGC");
return 0;
}
a) College
b) CGC
c) Compile time error
d) None of above
Answer: b
10.9 What will be the output of the program?
#include stdio.h
#include math.h
int main()
{
float x=2.69;
printf("%f, %f", ceil(x), floor(x));
return 0;
}
a) 2.000000, 1.000000
b) 3.000000, 2.000000
c) 3.000000, 0.000000
d) None of above
Answer: b
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 11 Structure Union
11.1 What is the similarity between a structure, union and enumeration?
a) All of them let you define new values
b) All of them let you define new data types
c) All of them let you define new pointers
d) All of them let you define new structures
Answer: b
11.2 What will be the output of the program ?
#include stdio.h
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0]=3;
u.ch[1]=2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
a) 3, 2, 515
b) 515, 2, 3
c) 3, 2, 5
d) 515, 515, 4
Answer: a
Explanation:
The system will allocate 2 bytes for the union.
The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.
Abbildung in dieser Leseprobe nicht enthalten
11.3 What will be the output of the following code?
#include stdio.h
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d\n", v.a);
return 0;
}
a) 10
b) 20
c) 30
d) 0
Answer: b
11.4 What will be the output of the following code?
#include stdio.h
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit={1, 2, 13};
printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
return 0;
}
a) 1 2 3
b) 2 4 4
c) -1 2 -3
d) -1 -2 -13
Answer: c
11.5 What will be the output of the following code?
#include stdio.h
int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
return 0;
}
a) -1, 0, 1, 2, 3, 4
b) -1, 2, 6, 3, 4, 5
c) -1, 0, 6, 2, 3, 4
d) -1, 0, 6, 7, 8, 9
Answer: d
11.6 What will be the output of the program ?
#include stdio.h
int main()
{
struct byte
{
int one:1;
};
struct byte var = {1};
printf("%d\n", var.one);
return 0;
}
a) 1
b) -1
c) 0
d) Error
Answer: -1
11.7 What will be the output of the program ?
#include stdio.h
int main()
{
enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
return 0;
}
a) -1, 0, 1, 2, 3, 4
b) Error
c) 0, 1, 6, 3, 4, 5
d) 0, 0, 6, 7, 8, 9
Answer: b
Explanation: Because ++ or – cannot be done on enum value.
11.8 Point out the error in the program?
int main()
{
struct emp
{
char name[20];
float sal;
};
struct emp e[10];
int i;
for(i=0; i =9; i++)
scanf("%s %f", e[i].name, e[i].sal);
return 0;
}
a) Error: Invalid structure member
b) Error: Floating point formats
c) No error
d) None of above
Answer: b
Explanation:
At run time it will show an error then program will be terminated.
Sample output: Turbo C (Windows)
c:\ myprogram
Sample
12.123
scanf : floating point formats not linked
Abnormal program termination
11.9 Point out the error in the program?
#include stdio.h
#include string.h
void modify(struct emp*);
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp e = {"Singh", 35};
modify(e);
printf("%s %d", e.name, e.age);
return 0;
}
void modify(struct emp *p)
{
p - age=p- age+2;
}
a) Error: in structure
b) Error: in prototype declaration unknown struct emp
c) No Error
d) None of the above
11.10 Which of the following statements correct about the below program?
#include stdio.h
int main()
{
struct emp
{
char name[25];
int age;
float sal;
};
struct emp e[2];
int i=0;
for(i=0; i 2; i++)
scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
for(i=0; i 2; i++)
scanf("%s %d %f", e[i].name, e[i].age, e[i].sal);
return 0;
}
a) Error: scanf() function cannot be used for structures elements.
b) The code runs successfully.
c) Error: Floating point formats not linked Abnormal program termination.
d) Error: structure variable must be initialized.
Answer: c
11.11 What will be the output of the program ?
#include stdio.h
int main()
{
union var
{
int a, b;
};
union var v;
v.a=10;
v.b=20;
printf("%d", v.a);
return 0;
}
a) 10
b) 20
c) 30
d) 40
Answer: b
11.12 Which of the following statements correctly assigns 45 to month using pointer variable pdt?
#include stdio.h
struct date
{
int day;
int month;
int year;
};
int main()
{
struct date d;
struct date *pdt;
pdt = d;
return 0;
}
a) pdt.month = 12
b) pdt.month = 12
c) d.month = 12
d) pdt- month = 12
Answer: d
11.13 What will be the output of the program?
#include stdio.h
int main()
{
struct value
{
int bit1:1;
int bit3:4;
int bit4:4;
}bit;
printf("%d", sizeof(bit));
return 0;
}
a) 1
b) 2
c) 3
d) 4
Answer: b
11.14 What will be the output of the program ?
#include stdio.h
int main()
{
enum status {pass, fail, absent};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = absent;
stud3 = fail;
printf("%d %d %d", stud1, stud2, stud3);
return 0;
}
a) 0, 1, 2
b) 1, 2, 3
c) 0, 2, 1
d) 1, 3, 2
Answer: c
11.15 Point out the error in the program?
#include stdio.h
int main()
{
struct bits
{
int i:40;
}bit;
printf("%d", sizeof(bit));
return 0;
}
a) 4
b) 3
c) Error: Bit field too large
d) None of above
Answer: c
11.16 Point out the error in the program?
#include stdio.h
int main()
{
struct emp
{
char n[20];
int age;
};
struct emp e1 = {"Singh", 23};
struct emp e2 = e1;
if(e1 == e2)
printf("The structure are equal");
return 0;
}
a) Prints: The structure are equal
b) Error: Structure cannot be compared using '=='
c) No Output
d) None of the above
Answer: b
11.17 What is the output of the following code?
struct {
short s[5];
union {
float y;
long z;
}u;
} t;
a) 22 bytes
b) 18 bytes
c) 20 bytes
d) 30 bytes
Answer: b
Explanation: Short array s[5] will take 10 bytes as size of short is 2 bytes. When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
11.18 What is the output of the following code?
# include iostream
# include string.h
using namespace std;
struct Test
{
char str[20];
};
int main()
{
struct Test st1, st2;
strcpy(st1.str, "Chandigarh");
st2 = st1;
st1.str[0] = 'C';
cout st2.str;
return 0;
}
a) Segmentation Fault
b) Error
c) Chandigarh
d) None of the above
Answer: c
Explanation : Array members are deeply copied when a struct variable is assigned to another one.
11.19 What is the output of the following code?
#include stdio.h
struct sample
{
int a=0;
char b='A';
float c=10.5;
};
int main()
{
struct sample s;
printf("%d,%c,%f",s.a,s.b,s.c);
return 0;
}
a) Error
b) 0,A,10.5
c) 0,A,10.500000
d) No Error , No Output
Answer: d
Explanation: We can only declare members inside the structure, initialization of member with declaration is not allowed in structure declaration.
11.20 What will be the output of following program ?
#include stdio.h
int main()
{
struct sample{
int a;
int b;
sample *s;
}t;
printf("%d,%d",sizeof(sample),sizeof(t.s));
return 0;
}
a) 12 12
b) 12 0
c) Error
d) 12 4
Answer: d
Explanation: There are 3 members with in structure (a,b,s), a and b is an integer variable that takes 4-4 bytes in memory, s is self-refrencial pointer of sample and pointer also takes 4 bytes in memory.
11.21 What will be the output of following program ?
#include stdio.h
struct sample
{
int a;
}sample;
int main()
{
sample.a=100;
printf("%d",sample.a);
return 0;
}
a) 0
b) 100
c) Error
d) Warning
Answer: b
11.22 What will be the output of following program ?
#include stdio.h
int main()
{
typedef struct tag{
char str[10];
int a;
}har;
har h1,h2={"IHelp",10};
h1=h2;
h1.str[1]='h';
printf("%s,%d",h1.str,h1.a);
return 0;
}
a) Error
b) IHelp, 10
c) IHelp,0
d) IHelp,10
Answer: b
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
9) https://www.careerride.com/
Chapter 12 Input and Output
12.1 What will be the content of 'file.c' after executing the following program?
#include stdio.h
int main()
{
FILE *fp1, *fp2;
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
fputc('A', fp1);
fputc('B', fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
a) B
b) A B
c) B B
d) Error opening the file.
Answer: b
12.2 Point out the error in the program?
#include stdio.h
#include stdlib.h
int main()
{
unsigned char;
FILE *fp;
fp=fopen("trial", "r");
if(!fp)
{
printf("Unable to open file");
exit(1);
}
fclose(fp);
return 0;
}
a) No error
b) unsigned int statement
c) unknown file pointer
d) None of above
Answer: a
12.3 Which of the following statement is correct about the program?
#include stdio.h
int main()
{
FILE *fp;
char ch;
int i=1;
fp = fopen("myfile.c", "r");
while((ch=getc(fp))!=EOF)
{
if(ch == '')
i++;
}
fclose(fp);
return 0;
}
a) The code counts number of characters in the file
b) The code counts number of lines in the file
c) The code counts number of words in the file
d) The code counts number of blank lines in the file
Answer: b
12.4 What will be the output of the program ?
#include stdio.h
int main()
{
int k=1;
printf("%d == 1 is" "%s", k, k==1?"TRUE":"FALSE");
return 0;
}
a) k == 1 is TRUE
b) 1 == 1 is TRUE
c) 1 == 1 is FALSE
d) K == 1 is FALSE
Answer: b
12.5 Point out the error in the program?
a) Error: suspicious char to in conversion in scanf()
b) Error: we may not get input for second scanf() statement
c) No error
d) None of above
Answer: b
12.6 Point out the error in the program?
#include stdio.h
int main()
{
FILE *fp;
fp=fopen("trial", "r");
fseek(fp, "20", SEEK_SET);
fclose(fp);
return 0;
}
a) Error: unrecognised Keyword SEEK_SET
b) Error: fseek() long offset value
c) No error
d) None of above
Answer: b
12.7 Will the following program work?
#include stdio.h
int main()
{
int n=5;
printf("n=%*d", n, n);
return 0;
}
a) No it will give an error
b) Yes it will give a value n=5
c) Yes it will give a value n=5 5
d) Yes it will run but doesn’t give a value
Answer: c
12.8 Will this program run ?
#include stdio.h
/* Assume there is a file called 'file.c' in c: c directory. */
int main()
{
FILE *fp;
fp=fopen("c: cfile.c", "r");
if(!fp)
printf("Unable to open file.");
fclose(fp);
return 0;
}
a) No error no output
b) Program crashes
c) Unable to open file
d) None of above
Answer: c
12.9 To print out a and b given below, which of the following printf() statement will you use?
#include stdio.h
float a=3.14;
double b=3.14;
a) printf("%f %lf", a, b);
b) printf("%Lf %f", a, b);
c) printf("%Lf %Lf", a, b);
d) printf("%f %Lf", a, b);
Answer: a
12.10 What will be the output of the program ?
#include stdio.h
int main()
{
float a=3.15529;
printf("%2.1f", a);
return 0;
}
a) 3.00
b) 3.15
c) 3.2
d) 3
Answer: c
12.11 What will be the output of the program ?
#include stdio.h
int main()
{
FILE *fp;
unsigned char ch;
/* file 'abc.c' contains "This is CGC " */
fp=fopen("abc.c", "r");
if(fp == NULL)
{
printf("Unable to open file");
exit(1);
}
while((ch=getc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
printf("", ch);
return 0;
}
a) This is CGC
b) This is
c) Infinite Loop
d) None of above
Answer: c
12.12 What will be the output of the program ?
#include stdio.h
int main()
{
char *p;
p="%d";
p++;
p++;
printf(p-2, 23);
return 0;
}
a) 21
b) 23
c) Error
d) No Output
Answer: b
12.13 What will be the output of the program ?
#include stdio.h
int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
return 0;
}
a) Print the contents of file "myfile.c"
b) Print the contents of file "myfile.c" upto NULL character
c) Infinite loop
d) Error
Answer: c
12.14 What does fp point to in the program ?
#include stdio.h
int main()
{
FILE *fp;
fp=fopen("trial", "r");
return 0;
}
a) The first character in the file
b) A structure which contains a char pointer which points to the first character of a file.
c) The name of the file
d) The last character in the file
Answer: b
Explanation:
The fp is a structure which contains a char pointer which points to the first character of a file.
12.15 Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp;
fp = fopen("NOTES.TXT", "r+");
a) Reading
b) Writing
c) Appending
d) Reading and Writing
Answer: d
Explanation:
r+ Open an existing file for update (reading and writing).
12. 16 On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include stdio.h
int main()
{
int i, fss;
char ch, source[20] = "source.txt", target[20]="target.txt", t;
FILE *fs, *ft;
fs = fopen(source, "r");
ft = fopen(target, "w");
while(1)
{
ch=getc(fs);
if(ch==EOF)
break;
else
{
fseek(fs, 4L, SEEK_CUR);
fputc(ch, ft);
}
}
return 0;
}
a) r n
b) Trh
c) err
d) None of above
Answer: b
Explanation:
The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".
Inside the while loop,
ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.
if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.
If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.
fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.
The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.
12.17 Out of fgets() and gets() which function is safe to use?
a) fgets()
b) gets()
Answer: a
Explanation: Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored.
12.18 What will be the output of the following code if the value 25 is given to scanf()?
#include stdio.h
int main()
{
int i;
printf("%d\n", scanf("%d", i));
return 0;
}
a) 25
b) 2
c) 1
d) 5
Answer: c
Explanation:
The scanf function returns the number of input is given.
printf("%d\n", scanf("%d", i)); The scanf function returns the value 1(one).
Therefore, the output of the program is '1'.
12.19 Which of the following statement is correct about the program?
#include stdio.h
int main()
{
FILE *fp;
char str[11], ch;
int i=0;
fp = fopen("INPUT.TXT", "r");
while((ch=getc(fp))!=EOF)
{
if(ch == '\n' || ch == ' ')
{
str[i]='\0';
strrev(str);
printf("%s", str);
i=0;
}
else
str[i++]=ch;
}
fclose(fp);
return 0;
}
a) The code writes a text to a file
b) The code reads a text files and display its content in reverse order
c) The code writes a text to a file in reverse order
d) None of above
Answer: b
Explanation This program reads the file INPUT.TXT and store it in the string str after reversing the string using strrev function.
References:
1) Test your C Skills by Yashwant Kanitkar
2) https://www.eduzip.com
3) https://www.gkseries.com
4) http://www.sanfoundry.com
5) http://www.indiabix.com
6) https://www.geeksforgeeks.org
7) https://astiwz.com
8) http://www.allindiaexams.in
Frequently Asked Questions About The C Language Preview
What is the main purpose of this C language preview document?
This document provides a comprehensive overview of the C programming language. It includes a table of contents, chapter summaries, objectives, key themes, and keywords, intended for academic use and analysis of C programming concepts.
What topics are covered in the table of contents?
The table of contents outlines the key areas discussed within the document, including declaration initializations, expressions, functions, pointers, strings, arrays, bitwise operators, control statements, pre-processors, floating-point issues, structures and unions, and input/output operations.
What does the 'Preface' section include?
The 'Preface' is a note to the students, describing the book, and a note of thanks.
What kind of questions are contained within each chapter?
Each chapter contains multiple-choice questions related to the topic, along with explanations for the correct answers. These questions are designed to test the reader's understanding of the material covered.
Are there code examples included?
Yes, many questions include code snippets demonstrating C programming concepts and testing the reader's understanding of expected outputs.
What is the significance of the external variables as described in "Chapter 1 Declaration Initializations"?
Chapter 1 explains the use of extern variables, specifically that the "extern" keyword specifies to the compiler that memory for the variable is allocated in another program, and an address will be assigned in the linking process. The code provides examples, too.
Why are variable names beginning with an underscore discouraged?
Variable names beginning with an underscore are discouraged to avoid conflicts with library routines that use such names.
Are case-sensitive differences important in C?
Yes, C is case-sensitive, which affects how variable names and keywords are interpreted.
What happens when an automatic structure is only partially initialized?
When an automatic structure is only partially initialized, the remaining elements are initialized to zero.
How do you define a preprocessor directive?
Preprocessor directives are defined using the # character. For instance, to define PI as 3.14 you write #define PI 3.14
What is the difference between extern int get(); and int get();?
extern int get(); tells that this function is in another source file.
What is the purpose of ceil(x) in the program?
The function ceil(x) rounds a floating-point number up to the nearest integer.
Is “Hello” the same as “Hello5” when printing?
No, the first printf statement will print Hello, due to the inner printf statement, and 5 as length due to the outer printf statement.
What is important when using doubles and floats in conditionals?
By default your 1.3 is double, so the answer in not equal if you use a float. If you take double then A is the answer. Now in this case your 1.3 is stored in floating point representation.
What will be the output of the following program? 1.18
#include stdio.h
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i 5; i++)
if ((char)a[i] == '5')
printf("%d\n", a[i]);
else
printf("FAIL\n");
}
The answer is FAIL printed 5 times, since the ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
How does sizeof work in the main() function?
Sizeof determines the size of a type, for instance, 1.4 is a double which is treated as 8, while float is 4.
Can you define your header files in double quotes?
Yes, you can define your header files in double codes (“ ” ).
What is the purpose of extern in programming?
extern makes a global variable available for use in a project.
What is the default type of a real number?
By default a real number is treated as a double.
Can keywords such as IF and OF be variable identifiers?
No, keywords can't be taken as identifiers.
Does order matter when declaring the function disp()?
Yes, disp() is called before it is defined.
Which keyword will be used to prevent any changes in the variable within a C program?
const is a keyword constant in C program.
What is the purpose of "\r" in the program?
r is carriage return and moves the cursor back. Progra is replaced by Skills
What happens with integers close to max size, such as 32767, when they are incremented?
32768 is not supported, the answer is -32768. It wraps around!
Does the function print numbers in Hello5 where 5 is the length due to outer printf statement?
Yes. printf statement will print Hello, “Hello” due to inner printf statement and 5 as length due to outer printf statement.
What is the order of evaluation for relational, arithmetic, logical, and assignment operators?
The evaluation order is arithmetic (2), then relational (1), then logical (3), and then assignment (4) i.e. 2134.
What is the output of the following code?
main()
{
int a = 75;
printf (“%d%%”, a);
}
The answer is 75%.
Is the function int(*ptr[5])(); a pointer to a function?
No, ptr is array of pointer to function.
What is the output of main() { int i = abc(10); printf("%d", --i); } int abc(int i) { return(i++); }?
The answer is 9.
Which is correct with respect to size of the data types?
char int double
Two different operators would always have different Associativity. True or False?
False.
What will be the output of the code char *p; p = "Hello"; printf("%cn",**p);?
H.
The reason for using pointers in a C program is.. ?
All of the above. (1) Pointers allow different functions to share and modify their local variables.
(2) To pass large structures so that complete copy of the structure can be avoided
(3) Pointers enable complex “linked” data structures like linked lists and binary trees
What is the output of the following code? int i = 10; void *p = i; printf("%d\n", (int)*p);?
Compile time error.
What is the meaning of the following declaration? int(*ptr[5])();?
ptr is array of pointer to function.
What is the output of main() { char *p; p = "Hello"; printf("%cn",**p); }?
H.
What are three key string library functions?
strcat(), strrev(), and strcmp()
What is the syntax of multi-dimensional array?
The syntax should be such as:
int a[][] = {{1,2},{3,4}};
How are array elements always stored in memory?
Sequential memory locations
What happens when you run int a=3; for(;a;printf("%d ", a--);?
Prints 3 2 1
How many times the while loop will get executed if a short int is 2 byte wide?
255 times
In C, right shifting is equivalent to.. ?
Division by two, and left-shifting is equivalent to multiplying by 2.
Consider the given statement: int y = 10 ^ 2. What will be the value of y?
8
Bitwise | can be used to set a bit in number?
Yes.
What is the difference between main(){int i; int arr[5] = {2,3}; for (i = 0; i 5; i++) printf("%d ", arr[i]); return 0; } and main(){int i; int arr[5] = {2,3,0,0,0}; for (i = 0; i 5; i++) printf("%d ", arr[i]); return 0; }?
They are semantically equivalent, because arrays get filled up with 0.
Are we only able to print hello() once if we define int i = 0; if (i == 0) { printf("Hello"); continue; }?
No, compile time error. The use of the keyword "continue" is not applicable in the example given.
What is the output of this code: main() { double k = 0; for (k = 0.0; k 3.0; k++) printf("CGC"); }?
CGC will be printed thrice.
Which data type should you specify when you are trying to represent a number as a long double?
3.14L
A preprocessor directive is a message from compiler to a linker. Is this true or false?
False.
A preprocessor command
has # as the first character
Should you define iostream.h for log(36.0)?
No, instead you should include math.h
C preprocessors can have compiler specific features.
True.
What will be the content of 'file.c' after executing the following program?
fp1=fopen("file.c", "w");
fp2=fopen("file.c", "w");
The output is "B".
What are the file IO operations you can perform with "r+" permissions?
Reading and writing can both be performed.
- Quote paper
- Rupinder Singh (Author), 2018, Expertise Your C. Computers and Basics of C++, Munich, GRIN Verlag, https://www.grin.com/document/419270