FAQs in C Language
FAQs in C Language
Edreena
CHAPTER 1
DATATYPES
Data types refer to an extensive system used for declaring variables or functions of
different types. The type of a variable determines how much space it occupies in storage and
how the bit pattern stored is interpreted.
The types in C can be classified as follows:
1. Basic Types:
They are arithmetic types and consists of the two types: (a) integer types and
(b) floating-point types.
2. Enumerated types:
They are again arithmetic types and they are used to define variables that can
only be assigned certain discrete integer values throughout the program.
3. The type void:
The type specifier void indicates that no value is available.
4. Derived types:
They include
(a) Pointer types,
(b) Array types,
(c) Structure types,
(d) Union types and
(e) Function types.
Integer Types
Following table gives you details about standard integer types with its storage sizes and value
ranges:
Storage
Type Value range
size
Floating-Point Types
Following table gives you details about standard floating-point types with storage sizes and
value ranges and their precision:
Type Storage size Value range Precision
1.typedef
Syntax:
2. enum
An enumerated type declares an optional type name and a set of zero or more
identifiers that can be used as values of the type. Each enumerator is a constant whose type is
the enumeration.
To create an enumeration requires the use of the keyword enum. The general form of
an enumeration type is:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
2
FAQ in C Language - Prepared by Ms.P.Edreena
Syntax:
8. What will be the value of d (assume d to be float) after the operation? d=2/7.0
A) 0
B) 0.2857
C) Cannot be determined
D) None of the above
Answer: B
{
int i;
for(i=0; i<=50000 ; i++)
printf(“ \n %d”, i);
}
11. What will be output when you will execute following c code?
#include<stdio.h>
int main()
{
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0;
}
Choose all that apply:
A) 421
B) 821
C) 441
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
5
FAQ in C Language - Prepared by Ms.P.Edreena
D) 841
E) 842
Answer: E
Explanation:
By default data type of numeric constants is:
6.5 : double 90000: long int ‘A’: char
In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of:
double is 8 byte
Long int is 4 byte
Character constant is 2 byte (size of char data type is one byte)
13. What will be output when you will execute following c code?
#include<stdio.h>
int main()
{
signed x;
unsigned y;
x = 10 +- 10u + 10u +- 10;
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
6
FAQ in C Language - Prepared by Ms.P.Edreena
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);
return 0;
}
Choose all that apply:
A) 00
B) 65536 -10
C) 0 65536
D) 65536 0
E) Compilation error
Answer: A
14. What will be output when you will execute following c code?
#include<stdio.h>
int main(){
double num=5.2;
int var=5;
printf("%d\t",sizeof(!num));
printf("%d\t",sizeof(var=15/2));
printf("%d",var);
return 0;
}
Choose all that apply:
A) 427
B) 445
C) 225
D) 247
Answer: C
Explanation:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
7
FAQ in C Language - Prepared by Ms.P.Edreena
sizeof(Expr) operator always returns the an integer value which represents the size of
the final value of the expression expr.
Consider on the following expression:
!num
=!5.2
=0
0 is int type integer constant and it size is 2 by in TURBO C 3.0 Consider on the
following expression:
var = 15/2
=> var = 7
=> 7
7 is int type integer constant.
Any expression which is evaluated inside the sizeof operator its scope always will be
within the sizeof operator. So value of variable var will remain 5 in the printf statement.
15. What will be output when you will execute following c code?
#include<stdio.h>
int main()
{
const int *p;
int a=10;
p=&a;
printf("%d",*p);
return 0;
}
Choose all that apply:
A) 0
B) 10
C) Garbage value
D) Error: Cannot modify const object
Answer: B
17. What will be output when you will execute following c code?
#include<stdio.h>
int main(){
int a= sizeof(signed) +sizeof(unsigned);
int b=sizeof(const)+sizeof(volatile);
printf("%d",a+++b);
return 0;
}
Choose all that apply:
A) 10
B) 9
C) 8
D) Error: Cannot find size of modifiers
Answer: C
Answer: B
19. What will be output when you will execute following c code?
#include<stdio.h>
int main()
{
volatile int a=11;
printf("%d",a);
return 0;
}
Choose all that apply:
A) 11
B) Garbage
C) We cannot predict
D) Compilation error
Answer: C
20. What is the range of signed int data type in that compiler in which size of int is two byte?
A) -255 to 255
B) -32767 to 32767
C) -32768 to 32768
D) -32768 to 32767
Answer: D
21. What will be output when you will execute following c code?
#include<stdio.h>
const enum Alpha{
X,
Y=5,
Z
}p=10;
int main()
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
10
FAQ in C Language - Prepared by Ms.P.Edreena
{
enum Alpha a,b;
a= X;
b= Z;
printf("%d",a+b-p);
return 0;
}
Choose all that apply:
A) -4
B) -5
C) 10
D) 11
Answer: A
Explanation:
Default value of enum constant X is zero and
Z=Y+1=5+1=6
So, a + b – p
=0 + 6 -10 = -4
22. What will be output when you will execute following c code?
#include<stdio.h>
int main(){
char a=250;
int expr;
expr= a+ !a + ~a + ++a;
printf("%d",expr);
return 0;
}
Choose all that apply:
A) 249
B) 250
C) 0
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
11
FAQ in C Language - Prepared by Ms.P.Edreena
D) -6
Answer: D
24. What will be output when you will execute following c code?
#include<stdio.h>
int main()
{
int a=-5;
unsigned int b=-5u;
if(a==b)
printf("Avatar");
else
printf("Alien");
return 0;
}
Choose all that apply:
A) Avatar
B) Alien
C) Run time error
D) Error: Illegal assignment
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
12
FAQ in C Language - Prepared by Ms.P.Edreena
Answer: A
Explanation:
int a=-5;
unsigned int b=-5u;
Constant -5u will convert into unsigned int. Its corresponding unsigned int value will
be :
65536 – 5 + 1= 65532
So, b = 65532
In any binary operation of dissimilar data type for example: a == b.Lower data type
operand always automatically type casted into the operand of higher data type before
performing the operation and result will be higher data type.
In c signed int is higher data type than unsigned int. So variable b will automatically
typecast into signed int. So corresponding signed value of 65532 is -5. Hence, a==b.
26. What will be output when you will execute following c code?
#include<stdio.h>
enum A{
x,y=5,
enum B{
p=10,q
}varp;
}varx;
int main(){
printf("%d %d",x,varp.q);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
13
FAQ in C Language - Prepared by Ms.P.Edreena
return 0;
}
Choose all that apply:
A) 0 11
B) 5 10
C) 4 11
D) 0 10
E) Compilation error
Answer: E
printf("FAIL\n");
}
A) The compiler will flag an error
B) Program will compile and print the output 5
C) Program will compile and print the ASCII value of 5
D) Program will compile and print FAIL for 5 times
Answer: D
Explanation:The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
29. The format identifier ‘%i’ is also used for _____ data type?
A) char
B) int
C) float
D) double
Answer: B
30. Which data type is most suitable for storing a number 65000 in a 32-bit system?
A) short
B) int
C) long
D) double
Answer: A
B) 8 Bytes
C) Depends on the system/compiler
D) Cannot be determined
Answer: C
C) 9
D) 10
Answer: B
Explanation:010 is octal representation of 8.
const int p;
p = 4;
printf("p is %d", p);
return 0;
}
A) p is 4
B) Compile time error
C) Run time error
D) p is followed by a garbage value
Answer: B
}
}
A) Does not print anything
B) Prints : Jan is the first month
C) Generates compilation error
D) Results in runtime error
Answer: B
CHAPTER 2
STORAGE CLASSES
In C language, each variable has a storage class which decides scope, visibility and lifetime
of that variable. The following storage classes are most oftenly used in C programming,
A variable declared inside a function without any storage class specification, is by default
an automatic variable. They are created when a function is called and are
destroyed automatically when the function exits. Automatic variables can also be called
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
21
FAQ in C Language - Prepared by Ms.P.Edreena
local variables because they are local to a function. By default they are assigned garbage
value by the compiler.
{
int Count;
auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used
within functions, i.e. local variables.
static can also be defined within a function. If this is done the variable is initialised at run
time but is not reinitialized when the function is called. This inside a function static variable
retains its value during various calls.
if(var)
main();
}
A) Prints 5 infinite times C) Runtime error E) 4 3 2 1
B) Compilation error D) 5 4 3 2 1
Answer: D
static storage class is given, it is initialized only once. The change in the value of a
static variable is retained even between the function calls. The function main () is also treated
like any other ordinary function, which can be called recursively.
}
A) 0 C) 20
B) Error D) garbage value
Answer: B
Explanation:
extern storage class in the following declaration,
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.
}
int out=100;
Answer: 100
Explanation:
This is the correct way of writing the previous program (Q.4).
void main( )
{
int a, b=0;
static int c [10]={1,2,3,4,5,6,7,8,9,0};
for (a=0; a<10;+ + a)
if ((c[a]%2)= = 0) b+ = c [a];
printf (“%d”, b);
}
A) 20 C) 25
B) 45 D) 90
Answer: A
printf statement will print b which is sum of the those values from array c which get
divided by 2, that is 2+4+6+8=20.
Explanation:
This is the correct way of writing the previous program.
becomes equal to 0, and since main() is recursively called, so the value of static i ie., 0 will be
printed every time the control is returned.
printf("%d\n",i);
{
int i=20;
printf("%d\n",i);
}
}
Answer: Linker Error: Unresolved external symbol i
Explanation:
The identifier i is available in the inner block and so using extern has no use in
resolving it.
15. A variable which is visible only in the function in which it is defined, is called
A) Static variable
B) Auto variable
C) External variable
D) Local variable
Answer: D
Answer: A
Answer: C
24. Default storage class if not any is specified for a local variable, is auto
A) true
B) false
C) Depends on the standard
D) None of the mentioned
Answer: A
Default storage classes for the locally declared variables are auto.
26. Array sizes are optional during array declaration by using ______ keyword.
A) auto
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
33
FAQ in C Language - Prepared by Ms.P.Edreena
B) static
C) extern
D) register
Answer: C
Answer: D
& (Address of) should not be used with register variable.
CHAPTER 3
OPERATORS
Types of operators
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
4. Assignment Operator
5. Increment & decrement Operator
6. Conditional Operator
7. Bitwise Operator
8. Special Operator
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then :
Relational Operator
Relational operators checks relationship between two operands. If the relation is true, it
returns value 1 and if the relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.
Logical Operators
Logical operators are used to combine expressions containing relation operators. In C, there
are 3 logical operators:
Meaning of
Operator Example
Operator
Meaning of
Operator Example
Operator
true.
Explanation
For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is
false in the given example. So, the expression is false. For expression ((c==5) || (d>5)) to be
true, either the expression should be true. Since, (c==5) is true. So, the expression is
true. Since, expression (c==5) is true, !(c==5) is false.
Conditional Operator
Conditional operator takes three operands and consists of two symbols ? and : . Conditional
operators are used for decision making in C. For example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value ofc will be -10.
Bitwise Operators
A bitwise operator works on each bit of data. Bitwise operators are used in bit level
programming.
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Other Operators:
1) Comma Operator
Comma operators are used to link related expressions together. For example:
int a,c=5,d;
It is a unary operator which is used in finding the size of data type, constant, arrays, structure
etc. For example:
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
Output
Conditional operators are used in decision making in C programming, i.e, executes different
statements according to test condition whether it is either true or false.
conditional_expression?expression1:expression2
If the test condition is true, expression1 is returned and if false expression2 is returned.
#include <stdio.h>
int main(){
char feb;
int days;
printf("Enter l if the year is leap year otherwise enter 0: ");
scanf("%c",&feb);
days=(feb=='l')?29:28;
Output
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Operators Categories:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
43
FAQ in C Language - Prepared by Ms.P.Edreena
2. For the following statements find the values generated for p and q.
#include<stdio.h>
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
44
FAQ in C Language - Prepared by Ms.P.Edreena
void main()
{
int p=0, q=1;
p=q++;
p=++q;
p=q--;
p=--q;
}
A) 1, 1 B) 0, 0 C) 3, 2 D) 1, 2
Answer : A
Explanation:
• Step 1: p=1 & q=2
• Step 2: p=3 & q=3
• Step 3: p=3 & q=2
• Step 4: p=1 & q=1
3.What are the values for variable, a & b when compiled without error?
#include<stdio.h>
#define MAX(x,y) ((x) > (y)) ? (x) : (y)
void main()
{
int a, b;
a = 3;
b = MAX (3, a++);
printf(“%d \t %d”, a, b);
}
Answer:
a = 5, b = 4
Explanation:
• b = ((3) > (a++)) ? (3) : (a++);
• i.e., if(3>3) // a=a+1; (a=4)
b=3;
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
45
FAQ in C Language - Prepared by Ms.P.Edreena
else
b=4; // a=a+1; (a=5)
In the end values are a = 5, b = 4
Note:
However you cannot give like --2. Because, -- operator can be applied to variables as
a decrement operator (e.g., i--). Value, 2 is a constant and not a variable.
• Since assignment operator (=) has more precedence than comma operator.
• So = operator will be evaluated first than comma operator. In the following expression
• x = 10, 20, 30, First 10 will be assigned to x then comma operator will be evaluated
}
Answer: False
Explanation:
As we know = is assignment operator not relation operator. So, a = 0 means zero will
assigned to variable a. In c zero represent false and any non-zero number represents true. So,
if(0) means condition is always false hence else part will execute.
• Note: if (a=10), o/p: true
int i=-1;
+i;
printf("i = %d, +i = %d \n",i,+i);
}
Answer:
i = -1, +i = -1
Explanation:
Unary + is the only dummy operator in C. Where-ever it comes you can just ignore it
just because it has no effect in the expressions (hence the name dummy operator).
printf("%d", k);
}
Output: 7
As Decimal: 65520
As Hexadecimal: FFF0 (0xFFF0)
As Binary: 1111111111110000
Note:
Decimal :15 => hex: f =>binary:1111
As Decimal: 65528 //( i.e.,6552=fff)
As Hexadecimal: FFF8 (0xFFF8)
As Binary: 1111111111111000
As Decimal: 65530
As Hexadecimal: FFFA (0xFFFA)
As Binary: 1111111111111010
int abc;
abc = a*b+c;
}
printf ("c = %d, abc = %d ", c, abc);
}
Answer:
c = 40 and abc = 0;
Explanation:
Because the scope of the variable 'abc' inside if(c) {.. } is not valid outside that if (.) { .. }.
}
void fn(int a, int b)
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
53
FAQ in C Language - Prepared by Ms.P.Edreena
{
printf("Fn : a = %d b = %d ", a, b);
}
Answer: The solution depends on the implementation of stack. (Depends on OS). In some
machines the arguments are passed from left to right to the stack. In this case the result will
be,
Main : 5 7 Fn : 7 7
Other machines the arguments may be passed from right to left to the stack. In that case
the result will be
Main : 6 6 Fn : 8 7
20. Which of the following statement is correct about the code snippet given below?
#include < stdio.h>
int main()
{
float z = 12.35, c = 10;
if( ++z%10 -z)
c += z;
else
c - = z;
printf( “%f %f”, z, c);
return 0;
}
A) The program will result in compile time error
B) The program will print 12.35 22.35
C) The program will print 13.35 22.35
D) The program will print 1.35 11.35
Answer: A
23. Which of the following statement is correct about the code snippet given below?
#include < stdio.h>
int main()
{
int a = 10, b = 2, c;
a = !( c = c == c) && ++b;
c += ( a + b- -);
printf( “ %d %d %d”, b, c, a);
return 0;
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
55
FAQ in C Language - Prepared by Ms.P.Edreena
Answer: A
Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011)
Now here variable a is a floating point variable while 0.7 is double constant. So variable a
will contain only 32 bit value i.e.
25. What will be output if you will compile and execute the following c code?
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
56
FAQ in C Language - Prepared by Ms.P.Edreena
void main()
{
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}
Answer: A
Explanation:
In ++a, ++ is pre increment operator. In any mathematical expression pre increment
operator first increment the variable up to break point then starts assigning the final value to
all variable.
Step 2: Start assigning final value 7 to all variable ‘i' in the expression.
x=++i + ++i + ++i;
x= 7 + 7 + 7
So, x=7+7+7=21
26. What will be output if you will compile and execute the following c code?
void main()
{
int a=10;
printf("%d %d %d",a,a++,++a);
}
(A) 12 11 11 (B) 12 10 10 (C) 11 11 12
(D) 10 10 12 (E) Compiler error
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
57
FAQ in C Language - Prepared by Ms.P.Edreena
Answer: A
Explanation:
In c printf function follows cdecl parameter passing scheme. In this scheme parameter
is passed from right to left direction.
printf("%d %d %d",a,a++,++a);
3 2 1
So first ++a will pass and value of variable will be a=10 then a++ will pass now value
variable will be a=10 and at the end a will pass and value of a will be a=12.
27. What will be output if you will compile and execute the following c code?
#define call(x,y) x##y
void main()
{
int x=5,y=10,xy=20;
printf("%d",xy+call(x,y));
}
(A) 35 (B) 510 (C) 15 (D) 40 (E) None of above
Answer: D
Explanation:
## is concatenation c preprocessor operator. It only concatenates the operands i.e.
a##b=ab
If you will see intermediate file then you will find code has converted into following
intermediate code before the start of actual compilation.
Intermediate file:
void main()
{
int x=5,y=10,xy=20;
printf("%d",xy+xy);
}
28. 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) Order may vary from compiler to compiler
D) None of above
Answer: C
Explanation:
Here, Multiplication will happen before the addition, but in which order the functions would
be called is undefined. In an arithmetic expression the parenthesis tell the compiler which
operands go with which operators but do not force the compiler to evaluate everything within
the parenthesis first.
A) 1, 2 B) 1, 3 C) 2, 4 D) 1, 2, 3
Answer: D
Explanation:
An operation with only one operand is called unary operation.
Unary operators:
! Logical NOT operator.
~ bitwise NOT operator.
sizeof Size-of operator.
&& Logical AND is a logical operator.
Therefore, 1, 2, 3 are unary operators.
30. 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
Explanation:
2. Arithmetic operators: *, /, %, +, -
1. Relational operators: >, <, >=, <=, ==, !=
3. Logical operators : !, &&, ||
4. Assignment operators: =
CHAPTER 4
Branching:
1) if statement
✓ This is the most simple form of the branching statements.
✓ It takes an expression in parenthesis and an statement or block of statements.
✓ if the expression is true then the statement or block of statements gets executed
otherwise these statements are skipped.
or
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
or
if (expression)
{
Block of statements;
}
else if(expression)
{
Block of statements;
}
else
{
Block of statements;
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
61
FAQ in C Language - Prepared by Ms.P.Edreena
2) ? : Operator
✓ The ? : operator is just like an if ... else statement except that because it is an operator
you can use it within expressions.
✓ ? : is a ternary operator in that it takes three values, this is the only ternary operator C
has.
✓ ? : takes the following form:
Syntax:
if condition is true ? then X return value : otherwise Y value;
(OR)
cond ? X : Y
3) switch statement:
✓ The switch statement is much like a nested if .. else statement.
✓ Its mostly a matter of preference which you use, switch statement can be slightly
more efficient and easier to read.
Syntax:
switch( expression )
{
case constant-expression1: statements1;
[case constant-expression2: statements2;]
[case constant-expression3: statements3;]
[default : statements4;]
}
Using break keyword:
If a condition is met in switch case then execution continues on into the next case
clause also if it is not explicitly specified that the execution should exit the switch statement.
This is achieved by using break keyword.
Looping
Loops provide a way to repeat commands and control how many times they are
repeated. C provides a number of looping way.
1) while loop
✓ A while statement is like a repeating if statement.
✓ Like an If statement, if the test condition is true: the statments get executed.
✓ The difference is that after the statements have been executed, the test condition is
checked again. If it is still true the statements get executed again.
✓ This cycle repeats until the test condition evaluates to false.
Syntax:
while ( expression )
{
Single statement
or
Block of statements;
}
2) for loop
✓ for loop is similar to while, it's just written differently.
✓ for statements are often used to process lists such a range of numbers:
Syntax:
for( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}
i) expression1 - Initializes variables.
ii) expression2 - Conditional expression, as long as this condition is true, loop
will keep executing.
iii) expression3 - expression3 is the modifier which may be simple increment of a
variable.
3) do...while loop
✓ do ... while is just like a while loop except that the test condition is checked at the end
of the loop rather than the start.
✓ This has the effect that the content of the loop are always executed at least once.
Syntax:
do
{
Single statement
or
Block of statements;
}while(expression);
A) M.G.George B) M.Muralidaran
C) M.G.George Mohammad Abdul M.Vijay
D) Compilation error E) None of the above
Answer: C
Explanation:
If we don’t use break keyword in each case the program control passes to next
consecutive statements inside the switch condition.
void main()
{
switch(5||2|1)
{
case 3&2:printf("Anatomy of a Murder");
break;
case -~11:printf("Planet of Apes");
break;
case 6-3<<2:printf("The conversation");
break;
case 5>=5:printf("Shaun of the Dead");
}
}
Answer: E
Explanation:
Consider on the expression:
5||2|1
=5|| (2|1) //Bitwise or has higher precedence
=5||3
=1
Now, value of each case expression:
3&2 = 2
-~11 = -(-12) =12
6-3<<2 = 3 <<2 = 12
5>=5 = 1
case -~11 and case 6-3<<2 have same constant expression i.e. case 12
In c duplicate case is not possible.
Answer: A
Explanation:
In first iteration of for loop: j = 0
So, control will come to default,
i = -1
Due to continue keyword program control will move to beginning of for loop
In second iteration of for loop: j =2
So, control will come to case 2,
i+=2
i = i+2 = -1 +2 =1
Then come to case 4,
i%=2
i = i%2 = 1%2 = 1
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
68
FAQ in C Language - Prepared by Ms.P.Edreena
j= -1
Due to continue keyword program control will move to beginning of for loop. In third
iteration of for loop:
j = -1 +2 =1
So, control will come to case 1
i=2
In the fourth iteration of for loop:
j = 1+ 2 =3
So, control will come to default,
So i=1
In the fifth iteration of for loop:
j = 3 + 2 =5
So, control will come to default,
So i=0
In the sixth iteration of for loop:
j = 5 + 2 =7
Since loop condition is false. So control will come out of the for loop.
switch(97)
{
case 'A':str+=2;
case 'a':str+=4;
}
default:
for(;a;a--)
str+=8;
}
printf("%s",str);
}
A) cquestionbank.blogspot.com B) blogspot.com C) com
D) Compilation error E) None of the above
Answer: E
Explanation:
ASCII value of the character constant 'A' is 65 and 'a' is 97. Nesting of switch case is
possible in c.
A) 10 B) 11 C) 12 D) 1 E) Compilation error
Answer: D
Explanation:
Consider on the expression:
a=a==b==0;
a=(a==b)==0; //Since associate is right to left
a =(3==2)==0
a=0==0
a=1
switch case will not affect the value of variable a.
Also sizeof operator doesn't affect the value of the any variable
}
A) cquestionbank B) It will print nothing C) Run time error
D) Compilation error E) None of the above
Answer: D
Explanation:
Keyword break is not syntactical part of if-else statement. So we cannot use break
keyword in if-else statement. This keyword can be used in case of loop or switch case
statement.
Hence when you will compile above code compiler will show an error message:
Misplaced break.
11. In a for loop, if the condition is missing, then infinite looping cannot be avoided by a
A) continue statement B) goto statement
C) return statement D) break statement
Answer: A
13. What will be output when you will execute following c code?
#define PRINT printf("Star Wars");
printf(" Psycho");
#include<stdio.h>
void main()
{
int x=1;
if(x--)
PRINT
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
73
FAQ in C Language - Prepared by Ms.P.Edreena
else
printf("The Shawshank Redemption");
}
A) Stars Wars Psycho B) The Shawshank Redemption
C) Warning: Condition is always trueD) Warning: Condition is always false
E) Compilation error
Answer: E
Explanation:
PRINT is macro constant. Macro PRINT will be replaced by its defined statement just
before the actual compilation starts. Above code is converted as:
void main()
{
int x=1;
if(x--)
printf("Star Wars");
printf(" Psycho");
else
printf("The Shawshank Redemption");
}
If you are not using opening and closing curly bracket in if clause, then you can write
only one statement in the, if clause. So compiler will think:
(i) if(x--)
printf("Star Wars");
It is if statement without any else. It is ok.
(ii) printf(" Psycho");
It is a function call. It is also ok
(iii) else
printf("The Shawshank Redemption");
You cannot write else clause without any if clause. It is cause of compilation error.
Hence compiler will show an error message: Misplaced else
15. Which of the following comments about for loop are not correct?
A) Index value is retained outside the loop
B) Index value can be changed from within the loop
C) goto can be used to jump,out of loop
D) Body of the loop cannot be empty
Answer: D
Answer: D
#include <stdio.h>
int x;
void main()
{
if (x)
printf("hi");
else
printf("how are u");
}
A) hi B) how are you C) Compile time error D) None of the mentioned
Answer: B
scanf("%s", ch);
switch (ch)
{
case "1":
printf("1");
break;
case "2":
printf("2");
break;
}
}
A) 1 B) 2 C) Compile time error D) No Compile time error
Answer: C
break;
}
}
A) yes no B) yes C) no D) Compile time error
Answer: D
25. What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
int x=-1,y=-1;
if(++x=++y)
printf("XYZ");
else
printf("ABC");
}
Choose all that apply:
A) XYZ B) ABC
C) Warning: x and y are assigned a value that is never used
D) Warning: Condition is always true E) Compilation error
Answer: C, E
Explanation:
Consider following statement:
++x=++y
As we know ++ is pre increment operator in the above statement. This operator
increments the value of any integral variable by one and return that value. After performing
pre increments above statement will be:
0=0
In C language it is illegal to assign a constant value to another constant. Left side of =
operator must be a container i.e. a variable. So compiler will show an error message: Lvalue
required
In C if you assign any value to variable but you don’t perform any operator or
perform operation only using unary operator on the variable the compiler will show a
warning message: Variable is assigned a value that is never used
26. What will be the final values of I and j when the following code terminates?
void main()
{
static int I , j = 5;
for ( I = 0; I < 3; I ++)
{
printf(“%d%d\n”, I, j);
if( j > 1)
{
j - -;
main ();
}
}
}
A) 02 B) 11 C) 21 D) Error, because main cannot be called recursively
Answer: C
while(!-2);
}
while(0);
return 0;
}
int x=8;
Answer: 10
Explanation:
Here variable x is extern type. So it will search the definition of variable x. which is
present at the end of the code. So value of variable x =8.
There are two do-while loops in the above code. AS we know do-while executes at
least one time even that condition is false. So program control will reach at printf statement
at it will print octal number 10 which is equal to decimal number 8.
Note: %o is used to print the number in octal format.
In inner do- while loop while condition is ! -2 = 0
In C zero means false. Hence program control will come out of the inner do-while
loop. In outer do-while loop while condition is 0. That is again false. So program control
will also come out of the outer do-while loop.
A) 10 B) 5 C) 4 D) None
Answer: B
int main()
{
int i=2,j=2;
while(i+1?--i:j++)
printf("%d",i);
return 0;
}
Answer: 1
Explanation:
Consider the while loop condition: i + 1 ? -- i : ++j
In first iteration:
i + 1 = 3 (True)
So ternary operator will return -–i i.e. 1
In c 1 means true so while condition is true. Hence printf statement will print 1.
In second iteration:
i+ 1 = 2 (True)
So ternary operator will return -–i i.e. 0
In c zero means false so while condition is false. Hence program control will come
out of the while loop.
Answer: 24
Explanation:
Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf
statement will print 2
Loop incrimination: ++I i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf
statement will print 4.
Since is equal to for so if condition is also true. But due to break keyword program
control will come out of the for loop.
Answer: 5 2
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
84
FAQ in C Language - Prepared by Ms.P.Edreena
Explanation:
First iteration:
Loop initial value: r() = 7
Loop condition: r() = 6
Since condition is true so printf function will print r() i.e. 5
Loop incrimination: r() = 4
Second iteration:
Loop condition: r() = 3
Since condition is true so printf function will print r() i.e. 2
Loop incrimination: r() = 1
Third iteration:
Loop condition: r() = 0
Since condition is false so program control will come out of the for loop.
33. What will be output when you will execute following C code?
#include<stdio.h>
void main()
{
int x=1,y=2;
if(--x && --y)
printf("x=%d y=%d",x,y);
else
printf("%d %d",x,y);
}
&& operator returns 1 if and only if both expressions return a non-zero value
otherwise it && operator returns 0.
Property 2:
To optimize the execution time there is rule, Expression2 will only evaluate if and
only if Expression1 return a non-zero value.
In this program initial value of x is 1. So –x will be zero. Since -–x is returning zero
so -–y will not execute and if condition will be false. Hence else part will be executed.
34. What will be output if you will compile and execute the following c code?
void main()
{
float a=5.2;
if(a==5.2)
printf("Equal");
else if(a<5.2)
printf("Less than");
else
printf("Greater than");
}
(A) Equal (B) Less than (C) Greater than
(D) Compiler error (E) None of above
Answer: B
Explanation:
5.2 is double constant in c. In c size of double data is 8 byte while a is float variable.
Size of float variable is 4 byte.
So double constant 5.2 is stored in memory as:
101.00 11001100 11001100 11001100 11001100 11001100 11001101
Content of variable a will store in the memory as:
101.00110 01100110 01100110
It is clear variable a is less than double constant 5.2.
Since 5.2 is recurring float number so it different for float and double. Number likes
4.5, 3.25, 5.0 will store same values in float and double data type.
Note: In memory float and double data is stored in completely different way.
35. What will be output if you will compile and execute the following c code?
void main()
{
int x;
for(x=1;x<=5;x++);
printf("%d",x);
}
36. What will be output if you will compile and execute the following c code?
void main()
{
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else
if(x>i)
printf("Greater than");
else
printf("Less than");
}
CHAPTER 5
FUNCTIONS
A function is a group of statements that together perform a task. Every C program has
at least one function, which is main(), and all the most trivial programs can define additional
functions.
You can divide up your code into separate functions. How you divide up your code
among different functions is up to you, but logically the division usually is so each function
performs a specific task.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can
call. For example, function strcat() to concatenate two strings, function memcpy() to copy
one memory location to another location and many more functions.
A function is known with various names like a method or a sub-routine or a
procedure, etc.
Defining a Function:
The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list )
{
body of the function
}
Example:
Following is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum between the two:
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations:
A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately.
A function declaration has the following parts:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
90
FAQ in C Language - Prepared by Ms.P.Edreena
Calling a Function:
While creating a C function, you give a definition of what the function has to do. To
use a function, you will have to call that function to perform the defined task.
When a program calls a function, program control is transferred to the called function.
A called function performs defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns program control back to the main
program.
To call a function, you simply need to pass the required parameters along with
function name, and if function returns a value, then you can store returned value. For
example:
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
max() function is kept along with main() function and compiled the source code.
While running final executable, it would produce the following result:
Max value is: 200
Function Arguments:
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are
created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a
function:
By default, C uses call by value to pass arguments. In general, this means that code
within a function cannot alter the arguments used to call the function and above mentioned
example while calling max() function used the same method.
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %d\n", a, b );
swap( &a, &b ); // function call
printf("After: Value of a = %d and value of b = %d\n", a, b );
}
void swap( int *p1, int *p2 ) // function definition
{
int t;
t = *p2;
*p2 = *p1;
*p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %d\n", *p1, *p2 );
}
Output:
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 20 and value of b = 10
1. What is the following function computing? Assume a and b are positive integers.
int fn( int a, int b)
{
if (b == 0)
return b;
else
return (a * fn(a, b - 1));
}
A) Output will be 0 always B) Output will always be b
C) Computing ab D) Computing a + b
Answer: A
The output is always be 0 because b is decremented in recursive function fn each time
by 1 till the terminating condition b==0 where it will return 0.
2. A possible output of the following program fragment, when the input is “mix’, is
for (i=getchar();; i=get.char())
if (i==‘x’)
break;
else
putchar(i);
A) mi B) mix
C) mixx D) none of the above
Answer: D
Explanation:
As it is wrong syntax it is compiler error. Suppose if, i=get.char() is replaced by
getchar(), then the output will be ‘mi’.
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. Here 5*5 is multiplied 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.
Answer: A
for i=1, (i%2) is true so the statement will print 1; for for i=2, (i%2) is false so the
statement will print 2*i=2*2=4; for for i=3, (i%2) is again true so the statement will print 3;
for i=4, the statement is out from the loop.
9. How many times the following code prints the string “hello”.
for(i=1; i<=1000; i++);
printf(“hello”);
A) 1 B) 1000
C) Zero D) Syntax error
Answer: A
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
97
FAQ in C Language - Prepared by Ms.P.Edreena
The “for” loop is terminated by a semicolon so the next statement is execute that is
printing hello.
10. The function used to read a character from a file that has been opened in read mode is
A) putc B) getc
C) getchar D) putchar
Answer: B
getc is used to read a character from a file that has been opened in read mode.
11. The function that allocates requested size of bytes and returns a pointer to the first byte
of the allocated space is -
A) realloc B) malloc
C) calloc D) none of the above
Answer: B
malloc allocates requested size of bytes and returns a pointer to the first byte of the
allocated space.
13. In the following pgm add a stmt in the function fun such that the address of 'a' gets
stored in 'j'.
void main()
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
98
FAQ in C Language - Prepared by Ms.P.Edreena
{
int * j;
void fun(int **);
fun(&j);
}
void fun(int **k)
{
int a =0;
/* add a stmt here*/
}
Answer: *k = &a
The argument of the function is a pointer to a pointer.
14. What are the following notations of defining functions known as?
i. int abc(int a,float b)
{
/* some code */
}
ii. int abc(a,b)
int a; float b;
{
/* some code*/
}
Answer:
i. ANSI C notation
ii. Kernighan & Ritche notation
15. What are the following notations of defining functions known as?
void main()
{
int k=ret(sizeof(float));
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
99
FAQ in C Language - Prepared by Ms.P.Edreena
E) 2 1 2
Answer: A
foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
A)10
B)Some garbage value
C)Compile time error
D)Segmentation fault/code crash
Answer: C
Answer: A
Answer: A
int j = 2;
*p = &j;
printf("%d ", **p);
}
A)2 2
B)2 97
C)Undefined behaviour
D)Segmentation fault/code crash
Answer: A
23. Which type of variables can have same name in different function:
A) global variables
B) static variables
C) Function arguments
D) Both (B) and (C)
Answer: D
24. Arguments that take input by user before running a program are called?
A) main function arguments
B) main arguments
C) Command-Line arguments
D) Parameterized arguments
Answer: C
25. The maximum number of arguments that can be passed in a single function
are_____________
A) 127
B) 253
C) 361
D) No limits in number of arguments
Answer: B
Answer: A
Answer: B
Answer: C
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
106
FAQ in C Language - Prepared by Ms.P.Edreena
Answer: D
CHAPTER 6
ARRAYS
❖ An array is used to store a collection of data, but it is often more useful to think of an
array as a collection of variables of the same type.
❖ All arrays consist of contiguous memory locations.
❖ The lowest address corresponds to the first element and the highest address to the last
element.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows:
type arrayName [ arraySize ];
This is called a single-dimensional array.
where
arraySize must be an integer constant greater than zero and
type can be any valid C data type.
For example, to declare a 10-element array called balance of type double, use this statement:
double balance[10];
Initializing Arrays
❖ You can initialize array in C either one by one or using a single statement as follows:
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
❖ The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
❖ If you omit the size of the array, an array just big enough to hold the initialization is
created. Therefore, if you write:
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
❖ You will create exactly the same array as you did in the previous example. Following
is an example to assign a single element of the array:
balance[4] = 50.0;
❖ The above statement assigns element number 5th in the array with a value of 50.0.
❖ All arrays have 0 as the index of their first element which is also called base index and
last index of an array will be total size of the array minus 1.
2. What is the index number of the last element of an array with 29 elements?
A) 29 B) 28
C) 0 D) Programmer-defined
Answer: B
4. Which of the following correctly accesses the seventh element stored in foo, an array with
100 elements?
A) foo[6]; B) foo[7];
C) foo(7); D) foo;
Answer: A
5. Which of the following gives the memory address of the first element in array foo, an array
with 100 elements?
A) foo[0]; B) foo;
C) &foo; D) foo[1];
Answer: B
6. What will happen if in a C program you assign a value to an array element whose subscript
exceeds the size of array?
A) The element will be set to 0.
B) The compiler would report an error.
C) The program may crash if some important data gets overwritten.
D) The array size would appropriately grow.
Answer: C
{
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) 2, 1, 15 B) 1, 2, 5
C) 3, 2, 15 D) 2, 3, 20
Answer: C
}
A) 2, 3, 4, 5 B) 1, 2, 3, 4
C) 0, 1, 2, 3 D) 3, 2, 1 0
Answer: B
D) The program fails to compile because Address-of operator cannot be used with array name
Answer: A
Explanation: Name of array in C gives the address (except in sizeof operator) of the first
element. Adding 1 to this address gives the address plus the sizeof type the array has.
Applying the Address-of operator before the array name gives the address of the whole array.
Adding 1 to this address gives the address plus the sizeof whole array.
A) 1 B) 2
C) 6 D) Runtime Error
Answer: C
Explanation: &a is address of the whole array a[]. If we add 1 to &a, we get “base address of
a[] + sizeof(a)”. And this value is typecasted to int *. So ptr points the memory just after 6 is
stored. ptr is typecasted to “int *” and value of *(ptr-1) is printed. Since ptr points memory
after 6, ptr – 1 points to 6.
15. What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>
int main()
{
int arr[5], i=0;
while(i<5)
arr[i]=++i;
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
return 0;
}
A) 1, 2, 3, 4, 5, B) Garbage value, 1, 2, 3, 4,
C) 0, 1, 2, 3, 4, D) 2, 3, 4, 5, 6,
Answer: B
Explanation:
Since C is a compiler dependent language, it may give different outputs at different
platforms. We have given the TurboC Compiler (Windows) output.
Two-Dimensional Arrays:
❖ The simplest form of the multidimensional array is the two-dimensional array.
❖ A two-dimensional array is, in essence, a list of one-dimensional arrays.
❖ To declare a two-dimensional integer array of size x,y you would write something as
follows:
type arrayName [ x ][ y ];
where
type can be any valid C data type and
arrayName will be a valid C identifier.
❖ A two-dimensional array can be thought as a table which will have x number of rows
and y number of columns.
#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) Compiler Error in line ” int a[][] = {{1,2},{3,4}};
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
CHAPTER 7
POINTERS
❖ A pointer is a value that designates the address (i.e., the location in memory), of some
value.
❖ Pointers are variables that hold a memory location.
❖ There are four fundamental things you need to know about pointers:
1) How to declare them (with the address operator '&': int *pointer =
&variable;)
2) How to assign to them (pointer = NULL;)
3) How to reference the value to which the pointer points (known
as dereferencing, by using the dereferencing operator '*': value =
*pointer;)
4) How they relate to arrays (the vast majority of arrays in C are simple
lists, also called "1 dimensional arrays")
How to use Pointers?
There are few important operations, which we will do with the help of pointers very
frequently.
(a) Define a pointer variable
(b) Assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at
the address specified by its operand.
Following example makes use of these operations:
#include <stdio.h>
int main ()
{
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
When the above code is compiled and executed, it produces result something as follows:
● Address of var variable: bffd8b3c
● Address stored in ip variable: bffd8b3c
● Value of *ip variable: 20
NULL Pointers in C
❖ It is always a good practice to assign a NULL value to a pointer variable in case you
do not have exact address to be assigned. This is done at the time of variable
declaration. A pointer that is assigned NULL is called a null pointer.
❖ The NULL pointer is a constant with a value of zero defined in several standard
libraries. Consider the following program:
#include <stdio.h>
int main ()
{
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr );
return 0;
}
★ When the above code is compiled and executed, it produces the following result:
★ The value of ptr is 0
★ On most of the operating systems, programs are not permitted to access memory at
address 0 because that memory is reserved by the operating system. However, the
memory address 0 has special significance; it signals that the pointer is not intended to
point to an accessible memory location. But by convention, if a pointer contains the
null (zero) value, it is assumed to point to nothing.
★ To check for a null pointer you can use an if statement as follows:
★ if(ptr) /* succeeds if p is not null */
★ if(!ptr) /* succeeds if p is null */
C Pointers in Detail:
Pointers have many but easy concepts and they are very important to C programming.
There are following few important pointer concepts which should be clear to a C
programmer:
Concept Description
C - Pointer There are four arithmetic operators that can be used on pointers: ++,
arithmetic --, +, -
C - Array of pointers You can define arrays to hold a number of pointers.
Answer: D
The value of a = 4, b = address of a and c = Address of b. The statement **c can be
defined as *(*(Address of b)) = > *(Address of a) = > a.
Note:
*(Address of b) = Value of b (i.e.) Address of a
*(Address of a) = a
p1 = &a[4];
p2 = &a[6];
Which of the following statement is an incorrect w.r.t. pointer?
A) p1 + 2 B) p2 – 2
C) p2 + p1 D) p2 – p1
Answer: C
= > Addition of two pointers is not allowed.
4. If a function is declared as void fn(int *p), then which of the following statements is
valid to call function fn?
A) fn(x) where x is defined as int x;
B) fn(x) where x is defined as int *x;
C) fn(&x) where x is defined as int *x;
D) fn(*x) where x is defined as int *x;
Answer: B
Function void fn(int *p) needs pointer to int as argument. When x is defined as int
*x, then x is pointer to integer and not *x.
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j<5;j++)
{
printf(" %d ",*c);
++q;
}
for(j=0;j<5;j++)
{
printf(" %d ",*p);
++p;
}
}
Answer: 2222223465
Explanation:
Initially pointer c is assigned to both p and q. In the first loop, since only q is
incremented and not c , the value 2 will be printed 5 times. In second loop p itself is
incremented. So the values 2 3 4 6 5 will be printed.
gives a value of 1. As it needs two bytes to store the address of the character pointer sizeof(p)
gives 2 as output.
Variable, p is pointing to character '\n'. Variable, str1 is pointing to character 'a'. For
++*p, "p is pointing to '\n' and that is incremented by one", the ASCII value of '\n' is 10,
which is then incremented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that
is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98.
Now performing (11 + 98 – 32), we get 77("M"); So we get the output as 77. Suppose
if the format specifier used is %C the output would have been M.
printf("%s %s",p,p1);
}
Answer: ibj!gsjfoet
Explanation:
The statement, ++*p++ will parse in the given order:
Step 1: *p = > denotes the value at the address currently pointed by p.
Step 2: ++*p = > the retrieved value will be incremented.
Step 3: When, the end of the statement (;) is encountered the location will be incremented
(i.e.) p++ will be executed.
Hence, in the while loop initial value pointed by p is ‘h’, which is changed to ‘i’ by
executing ++*p. Pointer moves to point the second character, ‘a’ which is similarly changed
to ‘b’ and so on. Similarly blank space is converted to ‘!’. Thus, value in p becomes “ibj!
gsjfoet” and since p reaches ‘\0’ and p1 points to p so, p1doesnot print anything.
return 0;
}
Answer: H
Explanation:
* is a dereference operator and & is a reference operator. They can be applied any
number of times provided it is meaningful. Here p points to the first character in the string
"Hello". *p dereferences it and so its value is H. Again & references it to an address and *
dereferences it to the value H.
Note: If * and & comes together it can be just ignored.
17. What is the output for the following program? Assume the base address of ‘a’ as 100.
void main( )
{
int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}};
printf(“%u %u %u %d \n”,a,*a,**a,***a);
printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
133
FAQ in C Language - Prepared by Ms.P.Edreena
}
Answer:
100, 100, 100, 2
114, 104, 102, 3
Explanation:
The given array is a 3-D one. It can also be viewed as a 1-D array.
2 4 7 8 3 4 2 2 2 3 3 4
100 102 104 106 108 110 112 114 116 118 120 122
Thus, for the variables a, *a and **a in the first printf statement the address of first
element is displayed as output. The pointer variable, ***a displays the value at the base
address.
The expression, a+1 in the second printf statement, increments in the third dimension
thus points to value at 114, *a+1 increments in second dimension thus points to 104, **a+1
increments the first dimension thus points to 102 and ***a+1 first gets the value at first
location and then increments it by 1.
}
A) 10 20 30 40 50 10 20 30 40 50 B) 10 10 10 10 10 10 20 30 40 50
C) Compilation Error D) 10 20 30 40 50
Answer: C (Compiler error: lvalue required.)
Explanation:
Error is in line with statement a++. The operand must be an lvalue and may be of any
of scalar type for the any operator, array name only when subscripted is an lvalue. Simply
array name is a non-modifiable lvalue.
A) 0 1 2 B) 0 0 0 C) 1 1 1 D) Compilation error
123 111 222
Answer: C
Explanation:
Let us consider the array and the two pointers with some address
a
0 1 2 3 4
100 102 104 106 108
p
100 102 104 106 108
Answer: A
The variable i is a block level variable and the visibility is inside that block only. But
the lifetime of i is lifetime of the function so it lives up to the exit of main function. *j prints
the value stored in i since j points i.
r++;
printf("%p...%p...%p",p,q,r);
}
Answer: 0001...0002...0004
Explanation:
++ operator when applied to pointers increments address according to their
corresponding data-types.
{
int j = 10;
return &j;
}
A) 10
B) Compile time error
C) Segmentation fault/runtime crash
D) Undefined behavior.
Answer: A
Answer: A
If suppose the statement, foo(p++); is written as foo((&i)++); then it will end up with
compile time error.
CHAPTER 8
STRINGS
1. Which of the following function sets first n characters of a string to a given character?
A) strinit() B) strnset()
C) strset() D) strcset()
Answer: B
4. The library function used to find the last occurrence of a character in a string is
A) strnstr() B) laststr()
C) strrchr() D) strstr()
Answer: C
5. Which of the following function is used to find the first occurrence of a given string in
another string?
A)strchr() B)strrchr()
C)strstr() D)strnset()
Answer : C
6. Which of the following function is more appropriate for reading in a multi-word string?
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
144
FAQ in C Language - Prepared by Ms.P.Edreena
A) printf(); B) scanf();
C) gets(); D)puts();
Answer: C
A) A B) a
C) c D) 65
Answer: A
Explanation:
● char p[] = "%d\n"; The variable p is declared as an array of characters and initialized
with string "%d".
● p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes
"%c"
● printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
}
A) Good Morning B) Good
C) M D) Morning
Answer: D
Explanation:
printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
Hence the output is "Morning"
13. What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
char arr[7]="Network";
printf("%s",arr);
}
A) Network B) N
C) Garbage value D) Compilation error
Answer: C
Explanation:
Size of a character array should be one greater than the total number of characters in
any string which it stores. In c every string has one terminating null character. This represents
end of the string.
So in the string “Network” , there are 8 characters and they are
‘N’,’e’,’t’,’w’,’o’,’r’,’k’ and ‘\0’. Size of array arr is seven. So array arr will store only first
seven characters and it will note store a null character.
%s in printf statement prints the stream of characters until it gets the first null
character. Since array arr has not stored any null character so it will print garbage value.
14. What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
char arr[11]="The African Queen";
printf("%s",arr);
}
A) The African Queen B) The
C) The African D) Compilation error
Answer: D
Explanation:
Size of any character array cannot be less than the number of characters in any string
which it has assigned. Size of an array can be equal (excluding null character) or greater than
but never less than.
15. What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
int const SIZE=5;
int expr;
double value[SIZE]={2.0,4.0,6.0,8.0,10.0};
expr=1|2|3|4;
printf("%f",value[expr]);
}
A) 2.000000 B) 4.000000
C) 8.000000 D) Compilation error
Answer: D
Explanation:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
149
FAQ in C Language - Prepared by Ms.P.Edreena
16. What will be output when you will execute following c code?
#include<stdio.h>
void main()
{
char *ptr="cquestionbank";
printf("%d",-3[ptr]);
}
A) Garbage value B) -300
C) -101 D) Compilation error
Answer: C
Explanation:
-3[ptr]
=-*(3+ptr)
=-*(ptr+3)
=-ptr[3]
=-103 //ASCII value of character ‘e’ is 103
18. In below program, what would you put in place of “?” to print “Quiz”?
#include <stdio.h>
int main()
{
char arr[] = "GeeksQuiz";
printf("%s", ?);
return 0;
}
A) arr B) (arr+5)
C) (arr+4) D) Not possible
Answer: B
Explanation:
Since %s is used, the printf statement will print everything starting from arr+5 until it
finds ‘\0’
Explanation:
Let us consider the line below the for loop
● p[i] = s[length — i];
For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0′
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change
for i >0. Nothing is printed if we print a string with first character ‘\0′
19. Assume that a character takes 1 byte. What is the output of the following program?
#include<stdio.h>
int main()
{
char str[20] = "GeeksQuiz";
printf ("%d", sizeof(str));
return 0;
}
A) 9 B) 10
C) 20 D) Garbage Value
Answer: C
Explanation:
The sizeof() operator would return the size of the array. To get size of the string stored
in array, we need to use strlen().
A) 12 B) 120400
C) 1204 D) 1034
Answer: A
Explanation:
char s1[7] = "1234", *p;
p = s1 + 2; // p holds address of character 3
*p = '\0' ; // memory at s1 now becomes "12\034"
printf ("%s", s1); // All characters till \0 are printed
21. Output of following C program? Assume that all necessary header files are included
int main()
{
char *s1 = (char *)malloc(50);
char *s2 = (char*) malloc(50);
strcpy(s1, "strings");
strcpy(s2, "Quiz");
strcat(s1, s2);
printf("%s", s1);
return 0;
}
A) stringsQuiz B) stringss
C) strings Quiz D) Quiz
Answer: A
int i;
i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
printf("%d\n", i);
return 0;
}
A) 0 B) 1 C) 2 D) 4
Answer: A
24. If the size of pointer is 4 bytes then What will be the output of the program?
#include<stdio.h>
int main()
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
154
FAQ in C Language - Prepared by Ms.P.Edreena
Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be
12, 5, because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit
platform), the output will be 24, 5 (because the size of pointer is 4 bytes).
25. Which of the following statements are correct about the program below?
#include<stdio.h>
int main()
{
char str[20], *s;
printf("Enter a string\n");
scanf("%s", str);
s=str;
while(*s != '\0')
{
if(*s >= 97 && *s <= 122)
*s = *s-32;
s++;
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
155
FAQ in C Language - Prepared by Ms.P.Edreena
printf("%s",str);
return 0;
}
A) The code converts a string in to an integer
B) The code converts lower case character to upper case
C) The code converts upper case character to lower case
D) Error in code
Answer: B
CHAPTER 9
STRUCTURE
Structure is the collection of variables of different types under a single name for better
handling.
For example: You want to store the information about person about his/her name,
citizenship number and salary.
Structure Definition in C:
Keyword struct is used for creating a structure.
Syntax of Structure:
struct structure_name
{
data_type member1;
data_type member2;
..
data_type memeber;
};
Example:
struct person
{
char name[50];
int cit_no;
float salary;
};
UNIONS
Unions are quite similar to the structures in C. Union is also a derived type as
structure. Union can be defined in same manner as structures just the keyword used in
defining union in union where keyword used in defining structure was struct.
Example:
union car
{
char name[50];
int price;
};
}
A. Compile time error
B. Nothing
C. hello
D. Varies
Answer: A
Explanation: diff struct variables should not be assigned using "=" operator.
Explanation: When we declare a structure or union, we actually declare a new data type
suitable for our purpose. So we cannot initialize values as it is not a variable declaration but a
data type declaration.
Answer: B
Explanation: This type of initialization is called as Designated Initialization, which allows
the structure members to be initialized in any order. This feature is not available in C++ and
works only in C.
Answer: A
Explanation: Since x and arr[4] share the same memory, when we set x = 0, all characters of
arr are set as 0. O is ASCII value of ‘\0′. When we do “t.arr[1] = ‘G'”, arr[] becomes
“\0G\0\0″. When we print a string using “%s”, the printf function starts from the first
character and keeps printing till it finds a \0. Since the first character itself is \0, nothing is
printed.
Answer: C
Explanation:
When we declare a union, memory allocated for a union variable of the type is equal
to memory needed for the largest member of it, and all members share this same memory
space. In above example, “char arr[8]” is the largest member. Therefore size of union test is 8
bytes.
Answer: A
Explanation:
A structure cannot contain a member of its own type because if this is allowed then it
becomes impossible for compiler to know size of such struct. Although a pointer of same type
can be a member because pointers of all types are of same size and compiler can calculate
size of struct.
Struct
{
short s[5];
union
{
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8
bytes, respectively. The memory requirement for variable t, ignoring alignment
considerations, is
A. 22 bytes
B. 14 bytes
C. 18 bytes
D. 10 bytes
Answer: C
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).
};
struct node *s[10];
16. Assume that size of an integer is 32 bit. What is the output of following program?
#include<stdio.h>
struct st
{
int x;
static int y;
};
int main()
{
printf("%d", sizeof(struct st));
return 0;
}
A. 4
B. 8
C. Compiler Error
D. Runtime Error
Answer: C
Explanation: In C, struct and union types cannot have static members.
struct world
{
int a;
char b;
struct india
{
char c;
float d;
}p;
};
struct world st ={1,'A','i',1.8};
clrscr();
printf("%d\t%c\t%c\t%f",st.a,st.b,st.p.c,st.p.d);
getch();
}
Answer: 1 A I 1.800000
Explanation:
Nesting of structure is possible i.e. we can declare a structure within another structure
but it is necessary that an inner structure must declare a structure variable otherwise we can
not access the data member of the inner structure.
char b;
struct india orissa;
};
struct world st ={{1,2,3},'P','q',1.4};
clrscr();
printf("%d\t%c\t%c\t%f",st.a[1],st.b,st.orissa.c,st.orissa.d);
getch();
}
Answer: 2 p q 1.400000
19. Which of the following comment about the usage of structures in true?
A. Storage class can be assigned to individual member
B. Individual members can be initialized within a structure type declaration
C. The scope of the member name is confined to the particular structure, within which it
is defined.
D. None of above
Answer: C
Explanation:
Structure is user defined data type which is used to store heterogeneous data under
unique name.
A. String
B. Array
C. Structure
D. Files
Answer: C
Explanation:
Structure is a user defined data type which contains the variables of different data
types.
2. The size of a structure may not be equal to the sum of the size of its members.
A. Only option 1
B. Only option 2
C. Both option 1 and 2
D. Neither option 1 nor 2
Answer: C
CHAPTER 10
1. Fibonacci series:
We assume first two Fibonacci are 0 and 1. A series of numbers in which each
sequence number is sum of its two previous numbers is known as Fibonacci series and each
numbers are called Fibonacci numbers. So Fibonacci numbers is
Algorithm for Fibonacci series
Fn = Fn-2 + Fn-1
Example of Fibonacci series:
0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 ...
5 is Fibonacci number since sum of its two previous number i.e. 2 and 3 is 5
8 is Fibonacci number since sum of its two previous number i.e. 3 and 5 is 8 and so on.
Code 1:
#include<stdio.h>
int main()
{
int k,r;
long int i=0l, j=1, f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
Sample output:
Enter the number range: 15
FIBONACCI SERIES: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
return 0;
}
Sample output:
Enter the number range: 20
Fibonacci series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
610 987 1597 2584 4181
Code 1:
#include<stdio.h>
int main()
{
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
173
FAQ in C Language - Prepared by Ms.P.Edreena
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
Sample output:
Enter a number: 5
5 is a prime number
Code 2:
#include<stdio.h>
int main()
{
int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++)
{
count = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++;
break;
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
174
FAQ in C Language - Prepared by Ms.P.Edreena
}
if(count==0 && num!= 1)
printf("%d ",num);
}
return 0;
}
Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43
47
3. Factorial value
Factorial of number is defined as:
Factorial (n) = 1*2*3 … * n
For example: Factorial of 5 = 1*2*3*4*5 = 120
Note: Factorial of zero = 1
Code 1:
#include<stdio.h>
int main()
{
int i,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}
Sample output:
Enter a number: 5
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
175
FAQ in C Language - Prepared by Ms.P.Edreena
Sample output:
Enter a number: 8
Factorial of 8 is: 40320
4. Palindrome number
A number is called palindrome number if it is remain same when its digits are
reversed. For example 121 is palindrome number. When we will reverse its digit it will
remain same number i.e. 121
Examples: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121,
131, 141, 151, 161, 171, 181, 191 etc.
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
Sample output:
Enter a number: 131
131 is a palindrome
Sample output:
Enter a number: 25
25 is not a palindrome
while(num)
{
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Sample output:
Enter any number: 12
Reversed of number: 21
rev(num/10);
}
else
return 0;
return sum;
}
Sample output:
Enter any number: 456
Reverse of number: 654
Sample output:
Enter a number: 123
Sum of digits of number: 6
Sample output:
Enter a number: 45
Sum of digits of number: 9
7. Reverse String
Code 1:
#include<stdio.h>
int main()
{
char str[50];
char rev[50];
int i=-1,j=0;
printf("Enter any string : ");
scanf("%s",str);
while(str[++i]!='\0');
while(i>=0)
rev[j++] = str[--i];
rev[j]='\0';
printf("Reverse of string is : %s",rev);
return 0;
}
Sample output:
Enter any string : cquestionbank.blogspot.com
Reverse of string is : moc.topsgolb.knabnoitseuqc
sptr++;
i++;
}
while(i>=0)
{
sptr--;
*rptr = *sptr;
rptr++;
--i;
}
*rptr='\0';
printf("Reverse of string is : %s",rev);
return 0;
}
Sample output:
Enter any string : Pointer
Reverse of string is : retnioP
scanf("%d",&num);
if(big<num)
big=num;
}
printf("Largest number is: %d",big);
return 0;
}
Sample Output:
Enter the values of n: 3
Number 1: 12
Number 2: 32
Number 3: 35
Largest number is: 35
Code 2:
#include<stdio.h>
void main()
{
while(!printf("Hello world"))
{
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
184
FAQ in C Language - Prepared by Ms.P.Edreena
Code 3:
#include<stdio.h>
void main()
{
switch(printf("Hello world"))
{
}
}
Example 1: 153
Total digits in 153 is 3
AND 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2: 1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
185
FAQ in C Language - Prepared by Ms.P.Edreena
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
Sample output:
Enter a number: 153
153 is an Armstrong number
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
Output:
1 153 370 371 407
str3[i]='\0';
printf("After concatenation the strings are\n");
puts(str3);
return 0;
}
return 0;
}
In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10
Code 1:
#include<stdio.h>
int main()
{
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--)
{
if(x%i==0&&y%i==0)
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
191
FAQ in C Language - Prepared by Ms.P.Edreena
Code 2:
#include<stdio.h>
int main()
{
int n1,n2;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1>=n2-1)
n1=n1-n2;
else
n2=n2-n1;
}
printf("\nGCD=%d",n1);
return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 2 1 7
The array after sorting is: 0 4 5 7 2 1
The key process in quickSort is partition(). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x. All
this should be done in linear time.
Program:
#include<stdio.h>
void quicksort(int [10],int,int);
int main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
194
FAQ in C Language - Prepared by Ms.P.Edreena
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
195
FAQ in C Language - Prepared by Ms.P.Edreena
Complexity Analysis:
Worst Case Time Complexity : O(n2)
Best Case Time Complexity : O(n log n)
Average Time Complexity : O(n log n)
Space Complexity : O(n log n)
It is a simple Sorting algorithm which sorts the array by shifting elements one by one.
Following are some of the important characteristics of Insertion Sort.
3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a
partially sorted list, hence it increases its efficiency.
5. Its space complexity is less, like Bubble Sorting, inerstion sort also requires a single
additional memory space.
6. It is Stable, as it does not change the relative order of elements with equal keys
Program:
#include<stdio.h>
int main()
{
int i,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
196
FAQ in C Language - Prepared by Ms.P.Edreena
Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9
Complexity Analysis:
Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for
eg: an Array withN number of elements. Bubble Sort compares all the element one by one
and sort them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles up
towards the first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing
adjacent data items and swapping each pair that is out of order.
Program:
#include<stdio.h>
int main()
{
int s,temp,i,j,a[20];
printf("Enter total numbers of elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
//Bubble sorting algorithm
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
199
FAQ in C Language - Prepared by Ms.P.Edreena
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9
After sorting: 0 2 6 9 11
Complexity Analysis:
Best-case Time Complexity will be O(n), it is when the list is already sorted.
Merge Sort follows the rule of Divide and Conquer. But it doesn't divides the list into two
halves. In merge sort the unsorted list is divided into N sublists, each having one element,
because a list of one element is considered sorted. Then, it repeatedly merge these sublists, to
produce new sorted sublists, and at lasts one sorted list is produced.
Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort,
which means the "equal" elements are ordered in the same order in the sorted list.
Program:
#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main()
{
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}
Sample output:
Enter the total number of elements: 5
Enter the elements which to be sort: 2 5 0 9 1
After merge sorting elements are: 0 1 2 5 9
Complexity Analysis:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
203
FAQ in C Language - Prepared by Ms.P.Edreena
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
Binary Search is applied on the sorted array or list. In binary search, we first compare the
value with the elements in the middle position of the array. If the value is matched, then we
return the value. If the value is less than the middle element, then it must lie in the lower half
of the array and if it's greater than the element then it must lie in the upper half of the array.
We repeat this procedure on the lower (or upper) half of the array. Binary Search is useful
when there are large numbers of elements in an array.
Program:
#include<stdio.h>
int main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid])
{
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.
gets(str2);
printf("Before swaping the strings are\n");
puts(str1);
puts(str2);
while(str1[i]!='\0')
{
temp[j++]=str1[i++];
}
temp[j]='\0';
i=0,j=0;
while(str2[i]!='\0')
{
str1[j++]=str2[i++];
}
str1[j]='\0';
i=0,j=0;
while(temp[i]!='\0')
{
str2[j++]=temp[i++];
}
str2[j]='\0';
printf("After swaping the strings are\n");
puts(str1);
puts(str2);
return 0;
}
{
printf("%d",b[i]);
}
return 0;
}
25. Swap two variables without using third using c program variable
#include<stdio.h>
int main()
{
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
210
FAQ in C Language - Prepared by Ms.P.Edreena
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}
#include<stdio.h>
int main()
{
long int binaryNumber,decimalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0)
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
211
FAQ in C Language - Prepared by Ms.P.Edreena
remainder=binaryNumber%10;
decimalNumber=decimalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent decimal value: %ld",decimalNumber);
return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent decimal value: 13
3. push() function is used to insert new elements into the Stack and pop() is used to
delete an element from the stack. Both insertion and deletion are allowed at only one end
of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.
-1 Stack is Empty
Analysis of Stack:
Below mentioned are the time complexities for various operations that can be performed on the
Stack data structure.
Program:
#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION\n");
while (option)
{
printf (" 1 --> PUSH \n");
printf (" 2 --> POP \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("Enter your choice\n");
scanf ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?\n");
scanf ("%d", &option);
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
214
FAQ in C Language - Prepared by Ms.P.Edreena
}
/* Function to add an element to the stack */
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function to delete an element from the stack */
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
215
FAQ in C Language - Prepared by Ms.P.Edreena
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
Queue is also an abstract data type or a linear data structure, in which the first element is
inserted from one end called REAR(also called tail), and the deletion of exisiting element
takes place from the other end called as FRONT(also called head). This makes queue as
FIFO data structure, which means that element inserted first will also be removed first.
The process to add an element into queue is called Enqueue and the process of removal of an
element from queue is called Dequeue.
Analysis of Queue:
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
216
FAQ in C Language - Prepared by Ms.P.Edreena
Enqueue : O(1)
Dequeue : O(1)
Size : O(1)
Implementation of Queue
Queue can be implemented using an Array, Stack or Linked List. The easiest way of
implementing a queue is by using an Array. Initially the head(FRONT) and the tail(REAR)
of the queue points at the first index of the array (starting the index of array from 0). As we
add elements to the queue, the tail keeps on moving ahead, always pointing to the position
where the next element will be inserted, while the head remains at the first index.
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
217
FAQ in C Language - Prepared by Ms.P.Edreena
void display_queue();
int main()
{
int option;
printf(">>> c program to implement queue operations <<<");
do
{
printf("\n\n 1.Insert an element");
printf("\n 2.Delete an element");
printf ("\n 3.Display queue");
printf("\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: return 0;
}
}while(option!=4);
}
void insert_element()
{
int num;
printf("\n Enter the number to be inserted: ");
scanf("%d",&num);
if(front==0 && rear==MAX-1)
printf("\n Queue OverFlow Occured");
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
218
FAQ in C Language - Prepared by Ms.P.Edreena
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
void delete_element()
{
int element;
if(front==-1)
{
printf("\n Underflow");
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
219
FAQ in C Language - Prepared by Ms.P.Edreena
first = 0;
while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);
}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
221
FAQ in C Language - Prepared by Ms.P.Edreena
return 0;
}
void reversedisplay(struct node *head)
{
if (head != NULL)
{
reversedisplay(head->next);
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
222
FAQ in C Language - Prepared by Ms.P.Edreena
printf("%d\t", head->num);
}
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
void display(struct node *p)
{
while (p != NULL)
{
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
223
FAQ in C Language - Prepared by Ms.P.Edreena
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}
# include <stdio.h>
# include <conio.h>
void main()
{
int mat[10][10] ;
int i, j, row, col, sum = 0 ;
clrscr() ;
printf("Enter the order of the matrix : ") ;
scanf("%d %d", &row, &col) ;
printf("\nEnter the elements of the matrix : \n\n") ;
for(i = 0 ; i < row ; i++)
for(j = 0 ; j < col ; j++)
scanf("%d", &mat[i][j]) ;
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
224
FAQ in C Language - Prepared by Ms.P.Edreena
getch() ;
}
CHAPTER 11
COMPANIES CRITICAL SOLVED QUESTIONING PAPERS
Here are few solved questions of major companies who go for engineering colleges
recruitment drive.
1) Hexaware
2) CSC
int a;
a=20;
return a;
}
a) Missing parantheses in return statement
b) The function should be defined as int f(int a, int b)
c) Redeclaration of a
d) None of these
Ans: c
a) Equal
b) unequal
c) error
d) None Ans: b
a) 1 1
b) 1 2
c) 2 2
d) 2 1
Ans: b
a) 1 2 3
b) 6
c) error
d) ”123”
Ans:c
a) ffff
b) 0000
c) ffdf
d) ddff
Ans:c
a) 128
b) Garbage value
c) error
d) 0
Ans:a
9. If y of integer type, then the expression 3*(y-8)/9 and (y-8)/9*3 yields the same value
if
a) y is an even number
b) y is a odd number
c) (y-8) is an integer multiple of 9
d) (y-8) is an integer multiple of 3
Ans: c
12. The greatest negative number which can be stored in computer that has 8 bit word
length and uses 2’s compliment arithematic is
a) -256 b) -255 c) -128 d) -127
Ans:c
13. Which of the following pairs of regular expression are not equivalent?
a) (ab)*a and a(ba)*
b) (a+b)* and (a*+b)*
c) b*ab*a(a+b)*
d) all of these
Ans:d
15. If the address of the 8th element in a linked list of integers is 1022 then the address of
the 10th element is
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
229
FAQ in C Language - Prepared by Ms.P.Edreena
16. If max is a function that returns the larger of the two integers, given as arguments then
which of the following statements finds the largest of the three given numbers?
a) max (max(a,b), max(a,c))
b) max (max(a,b), max(b,c))
c) max (b,max(a,c))
d) all of these.
Ans:c
19. A function that can access private members of a class even though it is not a member
of the class itself is
a) A friend
b) An inline function
c) A private function
d) Never allowed in oops
Ans: a
20. The feature that allows the same operations to be carried out differently depending on
the objects is
a) Polymorphism
b) Polygamy
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
230
FAQ in C Language - Prepared by Ms.P.Edreena
c) Inheritance
d) Multitasking
21. The algorithm design technique used in the quick sort algorithm is
a) Dynamic programming
b) Backtracking
c) Divide and conquer
d) Greedy method
22. In a circular linked list organization, insertion of a record involves the modification of
a) No pointer
b) 1 pointer
c) 2 pointers
d) 3 pointers
23. The worst case time required to search a given element in a sorted linked list of length
n is
a) 0(1) b) 0(log2n) c) 0(n) d)0(n log2n)
Ans: c
24. The five items A,B,C,D and E are pushed in a stack, one after the other starting from
A. The stack is popped four times and each element is inserted in a queue. Then the
two elements are deleted from the queue and pushed back on the stack. Now one item
is popped from the stack. The popped item is
a) A b) B c) C d) D
Ans: d
Consider a logical address space of 8 pages of 1024 words mapped into memory of 32
frames
Ques 25 & 26 based on above assumptions
c) seriesparallel
d) direct
Ans:a
28. Which layer functions as a liaison between user support layers and network support
layers?
a) Network layer
b) Physical layer
c) Transport layer
d) Session layer
Ans:c
29. In session layer during data transfer the data stream responsible for the control
purpose is
a) Regular data
b) Typed data
c) Capability data
d) Extended data Ans:c
30. Which of the following requires a device driver?
a) Register
b) Cache
c) Main memory
d) Disk
Ans: d
31. Consider a machine with 64MB physical memory and 32 bit virtual address space. If
the page size is 4kb then the approximate size of the page table will be
a) 16MB
b) 8MB
c) 2MB
d) 24MB
Ans: c
33. The optimal page replacement algorithm will select the page that
a) Has not been used for the longest time in the past
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
232
FAQ in C Language - Prepared by Ms.P.Edreena
35. When a subroutine is called the address of the instruction following the CALL
instruction is stored in/on the
a) Stack pointer
b) Accumulator
c) Program counter
d) Stack
Ans: d
38. The minimum number of interchanges needed to convert the array 89, 19, 40, 17, 12,
10, 2, 5, 7, 11, 6, 9, 70 into a heap with maximum element at the root is
a) 1
b) 2
c) 3
d) None
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
233
FAQ in C Language - Prepared by Ms.P.Edreena
Ans:b
40. If each address space represents one byte of storage space how many address lines are
needed to access RAM chips arranged in 4*6 array where each chip is 8k*4bits
a) 13
b) 15
c) 16
d) 17 Ans:d
44. Which of the following four 4 bit numbers equal its 1 compliment?
a) 1010
b) 1000
c) No such number exists
d) None
Ans:d
46. A designer wants to design a point to point sub network with 10 routers of full duplex
line. Then the total number of lines among them would be
a) 10
b) 20
c) 45
d) 95
Ans: d
48. the value of an automatic variable that is declared but not initialized will be
a) 0
b) -1
c) Garbage
d) 2
49. What will be the length of the following string using strlen()?
char str[20]=”Hello\rWorld”;
a) 5
b) 6
PETMS, Chennai. Training, Placement & Overseas Education. 91-9840649653.
235
FAQ in C Language - Prepared by Ms.P.Edreena
c) 10
d) 11
51. What happens when an integer pointer (int *ptr) is increment (ptr=ptr+1)?
a) Its value incremented by 1
b) Pointer cannot be incremented
c) Segmentation fault
d) Pointer to the next element
Ans:a
a) 3 2 1
b) 1 3 2
c) 2 3 1
d) 3 1 2
Ans:c
d) None
Ans:d
56. Consider a set of n tasks with known runtime r1,r2,…rn. To be run on a uniprocessor
machine. Which of the following processor scheduling algorithm will result in the
maximum throughput?
a) Round robin
b) Shortest job first
c) Highest response ratio next
d) FCFS
Ans: b
else
printf(“I is not five”);
printf(“over”);
}
a) Syntax error
b) Execution error
c) Prints over
d) Execution termination without printing anything
64. Which of the following data structure is mainly used during shift reduce parsing?
a) Pointers
b) Arrays
c) Tokens
d) Queues
Ans:d
68. Give a situation where there is a list of grocery items out of which only one item can
be chosen. Which among the following uI element is the best choice?
a) Push button
b) Checkbox
c) Text field
d) Drop down list
Ans: b
3.A reserved area of the immediate access memory used to increase the
running speed of the computer program.
a) session memory
b) bubble memory
c) cache memory
d) shared memory
Ans: c
4.A small subnet that sit between a trusted internal network and an
untruster external network, such as the public internet.
a) LAN
b) MAN
c) WAN
d) DMZ
Ans: d
6.main(){
float fl = 10.5;
double dbl = 10.5
if(fl ==dbl)
printf("UNITED WE STAND");
else
printf("DIVIDE AND RULE")
}
7.main(){
static int ivar = 5;
printf("%d",ivar--);
if(ivar)
main();
}
8.main()
{
extern int iExtern;
iExtern = 20;
printf("%d",iExtern);
}
c) compile error
d) linker error
Ans: d
10.main()
{
void vpointer;
char cHar = 'g', *cHarpointer = "GOOGLE";
int j = 40;
vpointer = &cHar;
printf("%c",*(char*)vpointer);
vpointer = &j;
printf("%d",*(int *)vpointer);
vpointer = cHarpointer;
printf("%s",(char*)vpointer +3);
}
11.#define FALSE -1
#define TRUE 1
#define NULL 0
main() {
if(NULL)
puts("NULL");
else if(FALSE)
puts("TRUE");
else
puts("FALSE");
}
12.main() {
int i =5,j= 6, z;
printf("%d",i+++j);
}
13.main() {
int i ;
i = accumulator();
printf("%d",i);
}
accumulator(){
_AX =1000
}
What is output?
a)1
b)10
c)100
d)1000
Ans: d
14.main() {
int i =0;
while(+(+i--)!= 0)
i- = i++;
printf("%d",i);
}
15.main(){
int i =3;
for(; i++=0;)
printf(("%d",i);
}
16. main(){
int i = 10, j =20;
j = i ,j?(i,j)?i :j:j;
printf("%d%d",i,j);
}
17.main(){
extern i;
printf("%d\t",i);{
int i =20;
printf("%d\t",i);
}
}
int arr[10];
printf("Array dimension is %d",DIMension(arr));
}
What is output?
a) array dimension is 10
b) array dimension is 1
c) array dimension is 2
d) array dimension is 5
Ans: b
19. main(){
void swap();
int x = 45, y = 15;
swap(&x,&y);
printf("x = %d y=%d"x,y);
}
void swap(int *a, int *b){
*a^=*b, *b^=*a, *a^ = *b;
20.main(){
int i =257;
int *iptr =&i;
printf("%d%d",*((char*)iptr),*((char *)iptr+1));
}
What is output?
a)1, 257
b)257 1c)0 0d)1 1
Ans: d
21.main(){
int i =300;
char *ptr = &i;
*++ptr=2;
printf("%d",i);
}
What is output?
a) 556
b) 300
c) 2
d) 302
Ans: a
22.#include
main(){
char *str ="yahoo";
char *ptr =str;
char least =127;
while(*ptr++)
least = (*ptr
printf("%d",least);
}
24.void main(){
int I =10, j=2;
int *ip = &I ,*jp =&j;
int k = *ip/*jp;
printf("%d",k);
}
25.main(){
char a[4] ="GOOGLE";
printf("%s",a);
}
30.What action is taken when the processer under execution is interrupted by TRAP in
8085MPU?
a) Processor serves the interrupt request after completing the
execution of the current instruction.
b) processer serves the interrupt request after completing the current task.
c) processor serves the interrupt immediately.
d) processor serving the interrupt request depent deprnds upon the
priority of the current task under execution.
Ans: a
33.The OR gate can be converted to the NAND function by adding----gate(s)to the input of
the OR gate.
a) NOT
b) AND
c) NOR
d) XOR
Ans: a
35.An 8085 based microprocessor with 2MHz clock frequency,will execute the following
chunk of code with how much delay?
MVI B,38H
HAPPY: MVI C, FFH
SADDY: DCR C
JNZ SADDY
DCR B
JNC HAPPY
a) 102.3
b)114.5
c)100.5
d)120
36.In 8085 MPU what will be the status of the flag after the execution of the following chunk
of code.
MVI B,FFH
MOV A,B
CMA
HLT
a) S = 1, Z = 0, CY = 1
b) S = 0, Z = 1, CY = 0
c) S = 1, Z = 0, CY = 0
d) S = 1, Z = 1 ,CY = 1
37.A positive going pulse which is always generated when 8085 MPU begins the machine
cycle.
a) RD
b) ALE address latch enable…
c) WR
d) HOLD
Ans: b
38.When a ----- instruction of 8085 MPU is fetched , its second and third bytes are placed in
the W and Z registers.
a) JMP
b) STA
c) CALL
d) XCHG
Ans: c
39.what is defined as one subdivision of the operation performed in one clock period.
a) T- State
b) Instruction Cycle
c) Machine Cycle
d) All of the above
Ans: a
40.At the end of the following code, what is the status of the flags.
LXI B, AEC4H
MOV A,C
ADD HLT
a) S = 1, CY = 0, P = 0 , AC = 1
b) S =0 , CY = 1, P = 0,AC = 1
c) S = 0, CY = 1, P = 0 , AC = 1
d) S = 0, CY = 1, P = 1 , AC = 1
41.In 8051 micro controller what is the HEX number in the accumulator after the execution
of the following code.
MOV A,#0A5H
CLR C
RRC A
RRC A
RL A
RL A
SWAP A
a)A6
b)6A
c)95
d)A5.
Ans: a
44.K6 processor
a) Hitachi b) toshiba c) zilog d) AMD.
Ans: d
45. What is the control word for 8255 PPI,in BSR mode to set bit PC3.
a)0EH b)0FH c)07H d)06H.
Ans:c
46.The repeated execution of a loop of code while waiting for an event to occur is called
---------.The cpu is not engaged in any real productive activity during this period,and the
process doesn't progress towards completion.
a) dead lock b) busy waiting c) trap door d) none.
Ans: b
48. Either all actions are carried out or none are. users should not have to worry about the
effect of incomplete transctions.DBMS ensures this by undoing the actions of incomplete
transctions.this property is known as
a) Aggregation b) atomicity c) association d) data integrity.
ans : B…
49.------ algorithms determines where in available to load a program. common methods are
first fit,next fit,best fit.--------- algorithm are used when memory is full , and one process (or
part of a process) needs to be swaped out to accommodate a new program.The -------------
50.Trap door is a secret undocumented entry point into a program used to grant access
without normal methods of access authentication. A trap is a software interrupt,usually the
result of an error condition.
a)true b)false.
Ans: A
51. Given a binary search tree,print out the nodes of the tree
according t5o post order traversal.
4
/\
25
/\
13
a)3,2,1,5,4. b)1,2,3,4,5. c)1,3,2,5,4. d)5,3,1,2,4. Ans: C
d) none
d) retransmission
ans:A
58) a _________sub query can be easily identified if it contains any references to the parent
sub query columns in the _________ clause
A) correlated ,WHERE
b) nested ,SELECT
c) correlated,SELECT
d) none
59) Hybrid device that combines the features of both bridge and router is known as
a) router b) bridge c) hub d) brouter
64. In binary search tree which traversal is used for ascending order values
a) Inorder b)preorder c)post order d)none
Ans: a
65 .You are creating an index on ROLLNO colume in the STUDENT table.which statement
will you use?
a) CREATE INDEX roll_idx ON student, rollno;
b) CREATE INDEX roll_idx FOR student, rollno;
c) CREATE INDEX roll_idx ON student( rollno);
d) CREATE INDEX roll_idx INDEX ON student (rollno);
Ans: c
66.A________class is a class that represents a data structure that stores a number of data
objects
a. container b.component c.base d.derived
Ans: a
70.Let A and B be nodes of a heap,such that B is a child of A. the heap must then satisfy the
following conditions
a)key(A)>=key(B)
b)key(A)
c)key(A)=key(B)
d)none
73.Sentinel node at the beginning and /or at the end of the linkedlist
is not used to store the data
a) true
b) false
Ans:a
printf("%d",x);
}
a) 5 b) 10 c) Error d) Garbage value
Ans: c
80. According to ANSI specifications which is the correct way of declaring main() when it
receives command line arguments?
a) main(int argc, char *argv[]) b) main(argc,argv) int argc; char *argv[];
c) main() {int argc; char *argv[]; } d) None of the above
Ans:a