Escolar Documentos
Profissional Documentos
Cultura Documentos
19.
20.
21.
22.
23.
}
24.
25.
26.
27.
28.
29.
int main()
{
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
return(0);
}
30.
31.
What will be the output of the following program [NOTE : THE USER
INPUT IS:Dear Friends, What is the output?] :
int main()
{
char line[80]; // Max. length=80 Chars
scanf("%[^,]s",line);
printf("\n%s",line);
return(0);
}
32.
What will be the output of the following program [NOTE : THE USER
INPUT IS :A B C] :
int main()
{
char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0);
}
33.
What will be the output of the following program [NOTE : THE USER
INPUT IS:5 5.75] :
void main()
{
int i=1;
float f=2.25;
scanf("%d a %f",&i,&f);
printf("%d %.2f",i,f);
}
3
34.
What will be the output of the following program [NOTE : THE USER
INPUT IS :ABC DEF GHI] :
int main()
{
char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0);
}
35.
What will be the output of the following program [NOTE : THE USER
INPUT IS:CMeansSea Ocean Vast] :
int main()
{
char a[80],b[80],c[80];
scanf("%1s %5s %3s",a,b,c);
printf("%s %s %s",a,b,c);
return(0);
}
36.
What will be the output of the following program [NOTE : THE USER
INPUT IS :123456 44 544] :
int main()
{
int a,b,c;
scanf("%1d %2d %3d",&a,&b,&c);
printf("Sum=%d",a+b+c);
return(0);
}
37.
38.
2.
3.
4.
5.
CHAPTER -4.3
Arithmetic
Operators,
Relational
Operators,
Assignment
Operators,Logical Operators, Increment And Decrement Operators,
Conditional Operator, Bitwise Operators
ONE WORD ANSWERS
Q1) How many arithmetic operators exist in C ?
Ans: Five.
Q2) What are the different arithmetic operators?
Ans: The different arithmetic operators are:
+ (addition),
- (subtraction),
* (multiplication),
/ (division),
% (modulo division).
Q3) What is precedence of an operator?
Ans: Precedence of an operators decides the order in which different
operators are applied.
Q4) What is associativity ?
Ans: Associativity decides the order in which operands are associated with
operator.
Q5) How many types of associativity exist in C ?
Ans : Two types.
Q6) What are two types of associativity ?
Ans : Left to Right & Right to Left.
Q7) Which arithmetic operator/s has highest precedence?
Ans: * , / and %
Q8) Which arithmetic operator/s has lowest precedence?
Ans: + and Q9)
Ans: Three
Q21) What are the different logical operators?
Ans: && (LOGICAL AND),
|| (LOGICAL OR),
! (LOGICAL NOT)
Q22) What is the associativity of logical and (&&), logical or (||) ?
Ans: Left to Right
Q23) What is the associativity of logical not ?
Ans: Right to Left
Q24) What is the associativity of increment and decrement operators?
Ans: Right to Left
Q25) What is an increment operator?
Ans: The operator which automatically increments the the value by one.
Q26) How many types of increment operators exist in C ?
Ans: Two types.
Q27) What are the two types of increment operators exist in C ?
Ans : Post increment and pre increment.
Q28) Which is a pre / post increment operator?
Ans: ++
Q29) Give an example for usage of pre increment operator
Ans: ++a
Q30) Give an example for usage of post increment operator
Ans: a++
Q31) What is post increment operator?
Ans : An operator which increments the value of a variable after the operation
Q32) What is pre increment operator?
Ans : An operator which increments the value of a variable before the
operation.
Q33) What is a decrement operator?
Ans: The operator which automatically reduce the the value by one.
Q34) How many types of decrement operators exist in C ?
Ans: Two types.
8
int main()
{
int goto=5;
printf("%d",goto);
return 0;
}
a. goto
b.5
c.go to 5
d.compilation error
d.151
d.150
int main()
{
int class=150, public=25, private=30;
class = class >> private - public;
printf("%d",class);
return 0;
}
a.2
b. 1
c.45
d.257
OUTPUT PREDICTION
1. What will be output of the following program?
#include<stdio.h>
int main()
{
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
2. What will be output of the following program?
#include<stdio.h>
int main()
{
int a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
3. What will be output of the following program?
#include<stdio.h>
void main()
{
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
4. What will be output of the following program?
#include<stdio.h>
int main()
{
11
int a;
a=015 + 0x71 +5;
printf("%d",a);
return 0;
}
5. What will be output of the following program?
#include<stdio.h>
int main()
{
printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
return 0;
}
6. What will be output of the following program?
#include<stdio.h>
int main()
{
int x=100,y=20,z=5;
printf("%d %d %d");
return 0;
}
7. What will be output of the following program?
#include<stdio.h>
int main()
{
int a=2;
a=a++ + ~++a;
printf("%d",a);
return 0;
}
8. What will be output of the following program?
#include<stdio.h>
int main()
{
int a;
a=sizeof(!5.6);
printf("%d",a);
return 0;
}
9. What will be output of the following program?
#include<stdio.h>
12
int main()
{
float a;
(int)a= 45;
printf("%d,a);
return 0;
}
10. What will be output of the following program?
#include<stdio.h>
int main()
{
int i=5;
int a=++i + ++i + ++i;
printf("%d",a);
return 0;
}
ASSIGNMENT
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
13
CHAPTER 4.4
Decision making : The If Statement , The If Else Construct,
Nested If Statements, Switch Statement, The Conditional Operator
The IF Statement
The if statement is a decision making statement and used to control the
flow of execution of statements. It's general syntax is
if(test expression)
{
executable part;
}
Q1. When if block is executed?
Ans: Only when the test expression is true.
Q2. What is the other name of if statements?
Ans: Decission control statement.
The If Else Construct:
The if else statement is an extension of the simple if statement .It's
general syntax is
if(test expression)
{
true block statements;
}
else
{
false block statements;
}
if the test expression is true,then the true block statements are executed
otherwise the flase block statements are executed.
Q3. When will be the else part be executed?
Ans: Only when ifs text expression is false.
Nested If Statements:
When a series of decisions are involved we may have to use more than
one if statements. Then we use nested if statements. Its general syntax is
if(test condition 1)
{
if(test condition 2)
14
{
statements..
}
}
Q4. What is nested if statement ?
Ans. If statement within an if statement is known as nested if statement.
Q5. When will be the nested if be executed?
Ans: Nested ifs are executed only when all the testconditions are satisfied.
Switch Statement:
The switch statement test the value of a given expression against a list of
case values. When a match is found a block of statements associated with the
case is executed, its general syntax is
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
....
default:
default -block;
break;
}
Q6. When the default statements are executed?
Ans:
When the entered value is not among the switch case values then
default statements are executed.
Q7. Which type of data is allowed in switch case?
Ans:
int or char constant.
The Conditional Operator
The ternary operator( ?: ) is called as conditional operator and it works
like if else statement.
expression1? expression2 : expression3 ;
Q8. How many operands required for conditional operator?
Ans: Three operands .
15
2)
a. 0
5)
a. 5
b. Compile-Time Error
c.10
d. No Output
d. None of these
b. 4
c . Infinite Loop
18
d.No Output
CHAPTER -4.5
PROGRAM LOOPING:The For Statement, Nested For Loops, The While
Statement, The Do While Statement, The Break And Continue Statements
The For Statement
The for loop is a entry controlled loop that provides a more concise loop
control structure.
19
do
{
bodyof the loop;
}
while(test-condition);
Q1) what is the minimum execution time of do while loop.?
Ans: 1 time.
The break and continue statements
When a break statement is encountered in a loop, the loop is immediately
exited from the loop and the program continues with the statement immediately
following the loop.
while (test-condition)
{
if(-----------)
break;
----------------}
when it is necessary to skip a part of the loop we use continue statements.
its general syntax is
while(test-condition)
{
if(-----------)
continue;
----------------}
MULTIPLE CHOICE QUESTIONS
1)
d.Hi!Bye
{
printf("Hi!");
if (0 || -1)
printf("Bye");
}
a. No Output
3)
b. Hi!
C. Bye
d. Hi!Bye
a. Compile-Time error
b. Hi!
c. Bye
d. Hi!Bye
4)
d. Hi!Bye
5)
6)
d. Hi!Bye
a. No Output
7)
b. OK
c. Testing
d. TestingOK
d. No Output
d. -1 0
a. 0
8)
9)
d. None of these
printf("Failure");
}
a. No Output
b. Success
c. Failure
d. SuccessFailure
11)
d. 0x1000 0xf88
12)
d. Unpredictable
13)
d. Unpredictable
14)
15)
d. Unpredictable
if (d > c)
if (c > b)
printf("%d %d",d,c);
else if (c > a)
printf("%d %d",c,d);
if (c > a)
if (b < a)
printf("%d %d",c,a);
else if (b < c)
printf("%d %d",b,c);
}
a. 4 3 3 4
16)
b. 4 3 3 2
c. 4 32 3
d. 4 33 1
}
a. 4 32 3
b. 4 33 42 3
c. 4 3 3 4 2 3
17)
d. None of these
{
int a=1;
if (a)
printf("Test");
else;
printf("Again");
}
a. Again
19)
b. Test
c. Compile-Time Error
d. TestAgain
}
a. No Output
20)
b. Default
c. DefaultChoice1
d. None of these
}
}
a. Choice1
b. Choice2
c. Default
OUTPUT PREDICTION
(1)
(2)
(3)
d. None of these
}
return 0;
}
4)
(5)
(6)
27
(7)
(8)
(9)
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=5;i++);
printf("%d",i)
return 0;
}
What will be output of following c code?
#include<stdio.h>
int i=40;
extern int i;
int main()
{
do
{
printf("%d",i++);
}while(5,4,3,2,1,0);
return 0;
(10)
28
(11)
}
What will be output of following c code?
#include<stdio.h>
int main()
{
int i;
for(i=10;i<=15;i++)
{
while(i)
{
do
{
printf("%d ",1);
if(i>>1)
continue;
}while(0);
break;
}
}
return 0;
}
(12)
(13)
}
return 0;
}
ASSIGNMENT
Q.Write a C program to calculate the 200th triangular number.
Q.Write a C program to reverse the digits of given number.
Q.Write a C program to determine if a number is even or odd
Q.Write a C program to implement the sign functions
Q.Write a C program to print prime numbers below 100
Q.Write a C program to print multiplication table of the number entered by the
user,the table should be displayed in the
following form
29x1=29
29x2=58
: :
: :
Q.Write a C program to produce the following output
1
2 3
4 5 6
7 8 9 10
Q.Write a C program to recieve an integer and find its octal equivalent.
Q.Write a C program to illustrate nested loops.
Q.Write a C program to illustrate the nested if elses.
30
CHAPTER 4.6
WORKKING WITH ARRAYS:Defining An Array,Declaration And
Initializing The Array,Character Arrays,The Const Qualifier ,
Multidimensional Array,Variable Length Arrays
ONE WORD ANSWERS
Q1) What is an array?
Ans: Array is collection of homogeneous data.It stores data in contigeous
memory location.
Q2) How many types of arrays exist and what are they?
Ans: There are 3 types of arrays they are
a) one dimensional array
b) two dimensional array
c) multi dimensional array
Q3) What is an one dimensional array?
Ans: A list of items given in one variable name using only one subscript is
known as one dimensional array.
Example: int a[3]
Q4) What is two dimensional array?
Ans: Collection of homogeneous elements in rows and columns is known as
two-dimensional array.
Example: int matrix[3][3]
Q5) What is the general format of declaring an array?
Ans: datatype Arrayname[size]
note: the size must be an integer.
Q6) What is the value of starting index of an array?
Ans: zero
Q7) What is the use of const qualifier?
Ans: The compiler allows to associate the const qualifier with variables
whose values will not be changed by the program.
Q8)
What value is automatically assigned to those array elements that are not
explicitly initialized?
31
Ans: All of array elements automatically set to zero except those that have
been explicitly initialized with in array definitions.
Q9)
State the rule that determines the order in which initial values
are assigned to multi dimensional array elements?
Ans: the rule is that the last (right most) subscript increases most
rapidly and the first (left most) increases least rapidly.
2.
3.
4.
5.
6.
8.
9.
OUTPUT PREDICTION
1.
2.
33
3.
4.
5.
6.
7.
int i;
for(i=0;i<8;i++)
printf(" %d",Dhoni[i]);
}
8.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Write a C program to read 10 integer numbers and print their average,
minimum and max. numbers
35
CHAPTER -4.7
WORKING WITH FUNCTIONS:
Defining A Function, Arguments And Local Variables, Returning
Function Results, Function Calling, Declaring Return Types And
Argument Types,Top Down Programming, Functions And Arrays,
Global Variables, Automtic And Static Variables, Recursive Function .
ONE WORD ANSWERS
Q1) What is a function?
Ans: A function is a self-contained block of code that performs a particular
task.
Q2) How many types of functions exist in C,what are they?
Ans: There are two types of functions they are
a.User-defined functions
b.standard library functions.
Q3) What are the advantages of using functions?
Ans: a. The length of the source program can be reduced by using function.
b.It is easy to locate and isolate a faulty functions for further
investigations.
Q4) What is Function definition?
Ans: It is an independent program module that is especially written to
implement the requirements of the function.
Q5) What are the elements involved in function definition?
Ans: The six elements that are involved in a function definition are:
a.Function return type
b.Function name
c.Parameter list
d.Local variables
e.Function statements and
f.Return statement
Q6) What is function header?
Ans: The function type,function name and the parameter list together is called
a function header.
36
38
2.
3.
4.
5.
6.
7.
Which of the following types of variables remain alive for the entire life
time of the program?
a. extern
39
b. auto
c. register
d. static
8.
9.
10.
11.
12.
c.101
d.100
13.
14.
b.11 7
c.7
d.17
41
15.
16.
17.
18.
42
int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}
a.23
b.12
c.52
d.31
OUTPUT PREDICTION
1)
2)
3)
4)
printf("%d",add(add(a,b),add(a,b)));
}
int add(a,b)
int a,b;
{
return (a+b);
}
5)
6)
7)
else
funct1(--b);
}
int funct1(int a)
{
if (a == 0)
return a;
else
funct2(--a);
}
void main()
{
int a=7;
printf("%d",funct1(a));
}
8)
9)
10)
return c;
}
void main()
{
int a=7,b=9;
printf("%d",compute(ab));
}
11)
12)
13)
a=15;
printf("%d",a);
changeval(75);
printf("%d",a);
}
14)
15)
ASSIGNMENT
47
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
48
CHAPTER 5.1
Working With Structures: Defining A Structure,Functions And Structures,
Initializing Structures, Array Of Structures,Structures Containing
Structures,Structures Containing Arrays,Structure Variants
Structure:
A structure is a user defined datatype and it is used for handling a group
of logically related datatypes.Structures can be intialised either at
compiletime or at runtime.
ONE WORD ANSWERS
Q1) What is a structure?
Ans: A structure is a collection of heterogeneous data.
Q2) What is the general format of the structure?
Ans: The general format\ of a structure is
struct tag_name
{
datatype name1;
datatype name2;
:
:
:
};
where name1,name2......... are called as structure members.
Q3). What are the major differences between arrays and structures?
Ans: a. an array is a collection of similar datatypes where as the stuctrure is a
collection of different data types.
b. an array is derived datatype where as a structure is user defined
datatype.
Q4). How the members of a structure can be accessed?
Ans: The members of a structure can be accessed using the dot operator or
arrow operator.
Q5) How is the structure initialized at compiletime?
Ans: Structure at compiletime can be initialised in this way
struct Test
{
49
int a;
char c[10];
};
struct tag_name s1={10,"ravi"};
Q6). How is the structure initialised at runtime?
Ans: Structure at runtime can be initialised in this way
struct tag_name
{
int a;
char c[10];
};
struct tag_name s1;
scanf("%d,%c",&s1.a,s1.c);
Q7). What is nested structure?
Ans: A structure within a structure is called as nested structure.
Q8) Give an example of array of structures?
Ans: struct student s[100];
Q9.
2.
50
3.
4.
Which of the following expressions arre correct for accessing the 'num'
variable value of the ith element of a structure array 'student'
a. student[i].num
b. student.num[i]
c. student[i]_>num
d. None of the above
5.
6.
What comes after the closing brace when we use typedef definition
a. type_name;
b. type name;
c. type_type;
d. None of the above
7.
8.
9.
51
OUTPUT PREDICTION
1)
2)
3)
4)
5)
6)
8)
9)
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
void main()
{
temp a={111,777.007};
printf("%d %.3f\n",a.i,a.f);
a=alter(a,222,666.006);
printf("%d %.3f",a.i,a.f);
}
10)
11)
{
record temp;
}list[2]={1,2,3,4,5,6,7,8};
void main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j<2; j++)
for (k=1; k>=0; k--)
printf("%d",list[i].temp.arr[j][k]);
}
12)
13)
printf("%s",temp->str);
temp=temp->next;
}
}
14)
15)
{
char ch;
int i;
float f;
}tempA;
void main()
{
tempA.ch='A';
tempA.i=777;
tempA.f=12345.12345;
printf("%d",tempA.i);
}
16)
17)
18)
What are the largest values that can be assigned to each of the bit fields
defined in [Q017] .
58
19)
20)
21)
22)
void main()
{
struct bitfield
{
unsigned a:5;
unsigned c:5;
unsigned b:6;
}bit;
char *p;
struct bitfield *ptr,bit1={1,3,3};
p=&bit1;
p++;
printf("%d",*p);
}
23)
24)
25)
struct field
{
int a;
char b;
}bit;
struct field bit1={5,'A'};
char *p=&bit1;
*p=45;
printf("\n%d",bit1.a);
}
26)
Q.
Write a c program that compares two given dates to store the data use
structure that contains three members namely date, month and year. If
the dates are equal then display the message as equal otherwise unequal
Q.
Q.
Q.
Q.
CHAPTER -5.2
CHARACTER STRINGS: Array Of Characters, Variable Length
Character Strings,Escape Characters, Character Strings, Structures And
Arrays,Character Operations
ONE WORD ANSWERS
Q1. What is a string?
Ans: Sstring is a collection of characters.
Q2) Which is the last character of a string ?
Ans: Null character is the last char of a string
Q3). Write the general form of the declaration of a simple string?
Ans: char string_name[size];
Q4)
Other than the scanf function,what is the other function that can be used
to read the string?
Ans: gets() is the other function that can be used to read the string.
Q5) What is the funtcion we use to write the string on the screen?
Ans: printf() function by using format code %s.
Q6) What is getchar()?
Ans: it reads a charactor from the keyboard.
Q7) What is meant by concatenation?
Ans: The process of combining one string with another is known as string
concatneation.
Q8) What is meant by strcmp?
Ans: The process of comparing the one string into another is known as
strcmp.
Q9) Which header file is used for string operations?
Ans: string.h .
Q10) What is meant by strncmp?
62
Ans: This is a three parametre function that compares only 'n' characters of
both the given strings.
the general format of the function is
strncmp(s1,s2,n); .
2.
3.
4.
5.
6.
What value will strlen functions return for the string {'r','a','m','\0'}
a. 3
b. 4
c. 5
d. None of the above
7.
Which of the following is the correct syntax for copying a string S1 into
S2?
63
a. strcmp(S2,S1);
b. strcpy(S1,S2);
c. strcmp(S1,S2);
d. strcpy(S2,S1);
8.
OUTPUT PREDICTION
1.
2.
3.
printf
#include<stdio.h>
enum power
{ Dalai, Vladimir=3, Barack, Hillary};
void main()
{
float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
enum power p=Barack;
printf("%0.f",leader[p>>1+1]);
}
5)
6)
7)
8)
9)
65
#include<stdio.h>
void main()
{
long myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
printf("%ld\t",myarr[1][2]);
printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
printf("%ld%ld%ld\t",*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
}
10)
11.
double myfun(double d)
{
d-=1;
return d;
}
13)
int *arr[3]={&a,&b,&c};
printf("%d",*arr[*arr[1]-8]);
}
16)
Q.
Write a c program to that will read a line and delete from it all the
occurences of the word "the"
Q.
Q.
Q.
69
CHAPTER- 5.3
POINTERS:Defining A Pointer Variable,Using Pointers In
Expressions,Pointers And Structures,Pointers And Functions,Pointers And
Arrays,Operation On Pointers,Pointer And Memory Address.
What the compiler does when the array name has been assigned to
pointer variable?
Ans: The compiler allocate the address of the first member of an array to the
pointer.
Q10) What is meant by call by value?
Ans: The process of passing th actual address of varibles to the function
is known as call by value.
Q11) Which operator is used to access the structure member when we use
pointer in structures?
Ans: Arrow operator(->) or member selection operator.
Q12) What are the disadvantages of the pointer?
Ans: a.Debugging becomes difficult task.
b.Direct access of memory loose data security
Q13) Which arithmetic operations are allowed in pointers?
Ans: Addition and Subtraction.
Q14) What is a near pointer?
Ans: A pointer which can store an address value between 0 to 65,535.
near pointer requires 2 bytes of memory.
Q15) What is a far pointer?
Ans: A pointer which can store an address value between 0 to 232-1.
far pointer requires four bytes of memory.
Q16) What is a generic pointer?
Ans: A pointer which can store any type of address, that pointer is known as
generic pointer.
Q17) How to represent a generic pointer ?
Ans: void * is used to represent generic pointer
Q18) what is wild pointer?
Ans: A pointer which is not initialized with any addresss is known as wild
pointer
Q19) What is single pointer?
71
2.
An integer pointer:
a. Points to the address of another integer value
b. Points to any data type
c. Points to itself
d. None of the above
3.
4.
5.
6.
Which of the following expressions will give the value stored in variable
X?
a. X
b. *x
C. *&X
d. &X
7.
If(int a1,*a2) a1=2 and a2=&a1 then what does a2++ depict(consider the
address value of a1 to be 3802,a1&a2 are integer pointers)
a. 3
b. 3803
c. 3804
d. 3802
8.
If a1=2 and a2=&a1 then what does a2++ depict(consider the address
value of a1 to be 3802,a1&a2 are char pointers)
a. 3
b. 3803
c. 3804
d. 3802
9.
If a1=2 and a2=&a1 then what does a2++ depict(consider the address
value of a1 to be 3802,a1&a2 are float pointers)
a. 3
b. 3803
c. 3804
d. 3806
10.
11.
12.
d. 1 & 1
13)
14)
15)
16)
17)
d. None of these
{
char far *p =(char far *)0x55550005;
char far *q =(char far *)0x53332225;
*p = 25;
(*p)++;
printf("%d",*q);
getch();
a. 25
18)
19)
}
b. Address c. Garbage d. Compilation error e. None of above
What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",++val,*ptr);
}
a.1235 1235
b.1235 1234 c. 1235 1236 d. 1235 1237
What will be the output of the following program :
void main()
{
int val=1234;
int* ptr=&val;
printf("%d %d",val,*ptr++);
}
a.1235 1235
b.1235 1234 c. 1234 1234 d. 1235 1237
20)
21)
22)
23)
24)
25)
26)
27)
d.777 777
{
int a=5u,*b,**c,***d,****e;
b=&a;
c=&b;
d=&c;
e=&d;
printf("%u %u %u %u",*b-5,**c-11,***d-6,65535+****e);
}
a. 0 65530 65535 5
c.0 65530 65535 4
28)
29)
30)
31)
b. 0 65530 65534 4
d. 0 65530 65502 5
d.5.75 5.75
d.69
d.69
printf("%d %d",*ptr++,(*ptr)++);
}
a.50
32)
33)
34)
b.96
d.69
d.56
35)
36)
}
a.162
354
b.163
325
c.158
356
d.569
589
OUTPUT PREDICTION
1)
2)
3)
k=&j;
printf(%u %u %d ,k,*k,**k);
}
4)
5)
6)
void main()
{
char arr[10];
arr = "world";
printf("%s",arr);
getch();
}
8)
What will be output if you will compile and execute the following c
code?
#include<stdio.h>
int main()
{
int a=5,b=10,c=15;
int *arr[]={&a,&b,&c};
printf("%d",*arr[1]);
return 0;
}
9)
10)
81
11)
12)
13)
14)
15)
int i = 3;
int *j;
int **k;
j = &i;
k = &j;
printf(%u %u %u,i,j,k);
}
16)
What will be output if you will compile and execute the following c
code?
#include<stdio.h>
int main()
{
static char *s[3]={"math","phy","che"};
typedef char *( *ppp)[3];
static ppp p1=&s,p2=&s,p3=&s;
char * (*(*array[3]))[3]={&p1,&p2,&p3};
char * (*(*(*ptr)[3]))[3]=&array;
p2+=1;
p3+=2;
printf("%s",(***ptr[0])[2]);
return 0;
}
ASSIGNMENT
Q.
Q.
Q.
Q.
Q.
Q.
Q.
Q.
CHAPTER 5.4
OPERATIONS ON BITS: BIT OPERATORS,BIT FIELD
Q1) What are bit-wise operators ex?
Ans: Bit wise AND(&),bit wise OR(|),bit wise exclusive OR(^),shift
left(<<),shift right(>>) and ones complement ( ~) are called bit
operators.
Q2)
84
CHAPTER -5.5
THE PREPROCESSOR: The #Define Statement, The ##Operator & #
Operator, The #Include Statement, Condition Compilation
Q1) What is #define directive?
Ans: A #define is a preprocessor compiler directive and not a statement.
Q2) What is the use of #define statement?
Ans: The use of #define statement is to assign symbolic names to program
constants.
Q3) Give some examples as how to use #define statement.
Ans: #define YES 1
#define NO 0
Q4) Would #define lines end with a semicolon(;)?
Ans: NO,#define lines donot end with a semicolon(;)
Q5)
What is a ## operator ?
Ans:
It is a token pasting operator, (Token merge, creates a single token from two
adjacent ones)
85
86
CHAPTER-5.6.
MORE ON DATA TYPES: Enumerated Data Types, The Typedef
Statement, Data Type Conversions
Q1) What is enum?
Ans: It is a special keyword which allows to define symbolic constants.
Q2) How is enum defined?
Ans: It
is
defined
as
follows
identifier{value1,value2,..................valuen};
enum
87
CHAPTER 5.7
INPUT AND OUTPUT OPERATIONS in c: Character I/O, Formatted
I/O, Input And Output Operations With Files, Special Functions For
Working With Files
Q1) What are character I/O functions?
Ans: putchar and getchar are character I/O functions
Q2) When do we use getchar?
Ans: getchar function is used when we want to read a single character at a
time.
Q3) What is the use of putchar?
Ans: It is used to display single character data
Q4) What are formatted I/O functions?
Ans: scanf and printf are formatted I/O functions
Q5) What is a file ?
Ans: File is collection of records ? It is stored in secondary storage area.
Q6) How many types files are exist in C ?what are they?
Ans: Two types ,they are: i) text file ii) binary file
Q7) What does a text file stores?
Ans: A text file stores different characters such as:
upper case,lower case english alphabets,numeric characters,
punctuation characters,special characters.
Q8) How to create a text file?
Ans: Text files can be created by using text editor ( like tc and notepad.)
Q9) How to declare and open a file for writting?
Ans: FILE *fp;
fp=fopen("file name","w");
88
90
CHAPTER 5.8
MISCELLANIOUS AND ADVANCED FUNCTIONS:The goto statement,
working with unions, the comma operator:
Q1) What is the use of goto statement?
Ans: goto is one type of unconditional jumping statements.
goto statement is used to branch unconditionally from one point to
another point in the specified block of a program
Q2) What is union?
Ans: A union, is a collection of variables of different types, just like a structure.
However, with unions, you can only store information in one field at any one
time
91
2)
3)
92
*ptr=25;
return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a. 25 40
b.25 25
c.40 25
d.35 35
4)
5)
6)
7)
a.20
b.10
c.30
d.39
8)
9)
10)
11)
12)
a.-1
b.65535
c.35689
d.56989
OUTPUT PREDICTION:
1)
2)
3)
4)
e = a ? b , c : c , d;
printf("%d",e);
}
5)
6)
ASSIGNMENT
Q.
Q.
Q.
Q.
Q.
Q.
Q.
96
PROGRAMMING PROBLEMS- 1
1)
c. 1 2 3 4
d. None of these
2)
d. 0 3 0
3)
d. 2
4)
5)
d. 2.000000
{
int a=5;
int b=6;;
int c=a+b;;;
printf("%d",c);;;;
}
a. Compile-Time Error
6)
b. Run-Time Error c. 11
d. None of these
7)
d. None of these
8)
9)
d. None of these
*&z*=*&x**&y;
printf("%d",z);
}
a. Compile-Time error
b. Run-Time Error
c. 24 d. Unpredictable
10)
11)
d. 1
13)
14)
d. No Output
int a=5,b=6;
printf("%d");
}
a. Compile-Time Error
b. 5
c. 6
d. Unpredictable
15)
d. Unpredictable
16)
d. 0
{
int i,n=10;
for (i=1; i<n--; i+=2)
printf("%d\n",n-i);
}
a. 84
b. 840
c. 852
PROGRAMMING PROBLEMS-2
d. 864
a. Compile-Time Error
2)
b. 5 3 2 (c)4 3 -1
d. 5 2 -2
a. Compile-Time Error
c. 5 100 105
b. Run-Time Error
d. None of these
3)
int a=5,b=10;
c=a+=b-=a;
printf("%d %d %d",a,b,c);
}
a. Compile-Time error
b. 5 10 5
c. 10 5 10
d. 10 10 5
5)
d. Infinite Loop
6)
7)
d. None of these
print(2);
}
a. Compile-Time error
c. Linker Error
b. x1=5.74 x2=23.78
d. None of these
9)
a. Compile-Time Error
c. Linker Error
11)
data=main()+val;
printf("%d ",data);
}
return 0;
}
a. Compile-Time Error
b. INFINITE LOOP c. 1 2 3 4 5 6
d. 0 0 0 0 0 0
13)
104
d. 5 7 1
17)
d. 6001 5001
18)
105
d. J E G D C B
PROGRAMMING PROBLEMS 3
a. Compile-Time Error
d. None of these
2)
c. 11 7
a. Compile-Time Error
3)
b. 11 25
b. 11 25
c. 11 7
d. 25 7
return *ptr;
return (a+b);
}
void main()
{
int var=7;
sum=func(&var,5,6);
printf("%d %d",sum,var);
}
a. Compile-Time Error
4)
a. 51
c. 11 7
d. 25 25
a. Compile-Time Error
d. None of these
5)
b. 11 25
b. "Friends"
c. Friends
c. 6
d. 1
6)
7)
d. 1
printf("%d",a);
}
a. 7
8)
a. 7
b. 8
c. 9
d. 10
c. 9
d. 10
9)
a. Compile-Time Error
d. None of these
11)
b. 2 2 2
c. 1 1 1
a. Compile-Time Error
12)
c. Infinite Loop
b. 2 2 2
c. 1 1 1
d. None of these
{
unsigned _=5;
_=_--- ++_;
printf("%d",_);
}
a. Compile-Time Error
b. 5
c. 65535
d. -1
13)
14)
d. -1
a. Compile-Time Error
15)
d. None of these
int a=1,b=2;
printf("%d",add(add(add(a,b),add(a,b)),add(add(a,b),add(a,b))));
}
a. Garbage value
17)
a.Compile-Time Error
18)
d. 12
b. 6
c. 7
d. 8
a. Compile-Time Error
b. 6
c. 7
d. None of these
19)
110
d. None of these
PROGRAMMING PROBLEM 4
MULTIPLE CHOICE QUESTIONS:
1)
a.
c.
Compile-Time Error
0.0 0.1 0.2 0.3 0.4
2)
A signed int bitfield 1-bit wide can only hold the values
a. 0 and 1
d. None of these
4)
b. 0 and -1
c. 0, 1 and -1
{
int a=19,b=4;
float c;
c=a/b;
printf("%f",c);
}
a. 4.75
b. 4
c. 4.750000
d. 4.000000
5)
a. 80 Characters
c. Until RETURN KEY
(i.e '\n') is encountered
b. 128 Characters
d. None of these
7)
112
9)
}
a.Compile-Time Error
10)
b. Default1Case1
c. Default2
d. Default1
a. 32767 cases
b. 257 cases
c. 127
d. None of these
11)
12)
a. Compile-Time Error
113
d. 5 0
b. First
Second
Third
c. FirstSecondThird
d. None of these
13)
d. 10 bytes
14)
114
d. None of these
Ans.
/* Swapping 3 numbers without using extra variable */
#include<stdio.h>
#include<conio.h>
void Swap(int *a,int *b,int *c)
{
*a = *a + *b + *c;
*b = *a - (*b + *c);
*c = *a - (*b + *c);
*a = *a - (*b + *c);
}
void main()
{
int x=1,y=2,z=3;
clrscr();
printf("BEFORE SWAPPING : %d %d %d\n",x,y,z);
Swap(&x,&y,&z);
printf("AFTER SWAPPING : %d %d %d",x,y,z);
} /* End of Main */
2)
W.A.P. in C to find the Fifth root of the sum of the squares of the first
100 ODD numbers only.
Ans.
/* To find the Fifth root of the sum of the squares of the first 100 ODD
numbers ONLY */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
115
long i,oddnum=1,sqrnum,sum=0;
clrscr();
for (i=1; i<=100; i++)
{
sqrnum=oddnum * oddnum; // Square the ODD number
sum+=sqrnum;
// Add Square value to the sum
oddnum+=2;
// Get the next ODD number
}
printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));
} /* End of Main */
3)
tot+=res*sum;
res=res*2;
}
mul2=tot;
}
printf("\n%3ld * %3ld = %3ld",mul,i-1,tot);
getch();
} /* End of Main */
4)
Ans.
/* Check whether the given number x is equal to the value 2 power i or
not using BITWISE
operators ONLY */
#include<stdio.h>
#include<conio.h>
void main(void)
{
long x;
clrscr();
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0)
printf("The given number %ld is EQUAL to the value 2
POWER something",x);
else
printf("The given number %ld is NOT EQUAL to the
value 2 POWER something",x);
getch();
} /* End of Main */
5)
Ans:
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include<stdio.h>
#include<conio.h>
#define MAX 10
int stack[MAX],top1,top2;
void init()
{
117
top1=-1;
top2=10;
}
void Push1(int item)
{
stack[++top1]=item;
}
void Push2(int item)
{
stack[--top2]=item;
}
int Pop1()
{
return stack[top1--];
}
int Pop2()
{
return stack[top2++];
}
void Display()
{
int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{
printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{
printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
void main()
{
int item,ch;
clrscr();
init();
while(1)
{
118
printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.
Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{
printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
default: printf("\nInvalid Choice");
}
119
}
} /* End of Main */
6)
W.A.P. in C that act as a guessing game in which the user has eight tries
to guess a randomly generated number. The program will tell the user
each time whether he guessed high or low. The user WINS the game
when the number guessed is same as randomly generated number.
Ans:
/* Guessing Game Solution */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i=8,rval,val,flag=1;
randomize();
// Initialize the random number generator
rval=random (100); // Generates a number in the range 0 to 90
printf("Welcome to Guessing Game.\n");
printf("RULES:\n1. Only 8 chances to guess the randomly
generated number.");
printf("\n2. You can WIN the game when the number guessed is
same as the randomly generated number.");
printf("\n3. Hints will be provided during the PLAY.");
printf("\n\n$$$ Good Luck. Start Guessing $$$");
for (i=1; i<=8; i++)
{
printf("\n\nGUESS %d ? ",i);
scanf("%d",&val);
if (val > rval)
printf("Your value is GREATER THAN the randomly
generated number");
else if (val < rval)
printf("Your value is LESSER THAN the randomly
generated number");
else
{
flag=1;
break;
}
}
if (flag)
printf("\n\n*** You are the WINNER. No. of tries = %d ***",i);
else
printf("\n\n*** You are the LOSER. ***");
120
} /* End of Main */
7)
Ans:
/* To determine how much money in a piggy bank */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(void)
{
float coin1=0.50,coin2=0.25,coin3=0.20,coin4=0.10,coin5=0.05,total=0.0;
int ncoins;
clrscr();
printf("How many 50 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin1);
printf("** %.2f **",total);
printf("\nHow many 25 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin2);
printf("** %.2f **",total);
printf("\nHow many 20 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin3);
printf("** %.2f **",total);
printf("\nHow many 10 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin4);
printf("** %.2f **",total);
printf("\nHow many 5 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin5);
printf("\n\nThe total amount is Rs.%.2f",total);
getch();
} /* End of Main */
121
8)
Modify the program given in [Q007] to accept total amount (in rupees)
and convert them into paise.(Vice-versa of [Q007])
Ans:
/* Denominations */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main(void)
{
int nc1,nc2,nc3,nc4,nc5,temp;
float total;
clrscr();
printf("Enter the amount : ");
scanf("%f",&total);
temp = total * 100;
nc1 = temp / 50;
temp = temp % 50;
nc2 = temp / 25;
temp = temp % 25;
nc3 = temp / 20;
temp = temp % 20;
nc4 = temp / 10;
temp = temp % 10;
nc5=temp;
printf("\n\nNo. of 50 paise coins = %d",nc1);
printf("\nNo. of 25 paise coins = %d",nc2);
printf("\nNo. of 20 paise coins = %d",nc3);
printf("\nNo. of 10 paise coins = %d",nc4);
printf("\nNo. of 5 paise coins = %d",nc5);
getch();
} /* End of Main *
9)
Ans:
/* 4-letter word unscrambling */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
122
int i,j,k,l,sum=6;
char *str;
clrscr();
printf("Enter a 4-letter word or string : ");
scanf("%s",str);
if (strlen(str) == 4)
{
printf("The possible combinations of the given 4-letter word is
shown.");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j)
{
for (k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
printf("\n%c%c%c
%c",str[i],str[j],str[k],str[l]);
}
}
printf("\nTotal combinations = %d",4*3*2*1);
}
else
printf("\nInvalid string. Length of the string must be 4-letters only ");
getch();
} /* End of Main */
123
IMPORTANT DIFFERENCES
1. Distinguish between getchar() and gets() functions.
getchar()
Used to receive a single character.
gets()
Used to receive a single string with
white spaces.
It requires a single argument.
scanf()
Strings with spaces cannot be
accessed.
All data types can be accessed.
Spaces and tabs are not acceptable as
a part of the input string.
Any number of characters, integers,
float etc. can be read.
gets()
Strings with any number of spaces
can be accessed.
Only character array data can be
accessed.
Spaces and tabs are perfectly
acceptable of the input string as a
part.
Only one string can be read at a time.
puts()
printf()
124
4. What is the difference between a pre increment and a post increment operation?
A pre-increment operation such as ++a, increments the value of a by 1, before a is
used for computation, while a post increment operation such as a++, uses the
current value of a in the calculation and then increments the value of a by 1.
Break
Continue
Do-while loop
Global variables
8. State the differences between the function prototype the function definition
Function prototype
Function Definition
125
Recursion
Recursion achieves repetition
through repeated function calls.
Recursion terminates when a base
case is recognized.
Recursion keeps producing simple
versions of the original problem
until the base case is reached.
Recursion causes another copy of
the function and hence a
considerable memory space is
occupied.
It increases the processors
operating time.
11. What is the difference between the address stored in a pointer and a value at the
address?
The address stored in the pointer is the address of another variable. The value
stored at that address is a stored in a different variable. The indirection operator
(*) returns the value stored at the address.
12. What is the difference between the indirection operator and the address of
operator?
126
The indirection operator (*) returns the value of the address stored in a pointer.
The address of operator (&) returns the memory address of the variable.
13. State the difference between call by value and call by reference.
Call by value
Call by reference
Formal parameter is a reference
variable.
It cannot change the actual parameter. It can change the actual parameter.
Actual parameter must be a
variable.
Structures
127
Example:
int sum [100];
Unions
All the members of a union use the
same memory space.
The keyword union tells us that we
are dealing with unions.
Only one member of a union can be
initialized.
Less memory space is required
since all members are stored in the
same memory locations.
Its general formula is:
union union name
{
data_type structure Member1;
data_type structure Member2;
.
.
data_type structure Member N;
} union variable;
Example:
union student
{
char studname[25];
int rollno;
} stud1;
128
129