Question Bank of CPU c Programming
-
Upload
palak-ariwala -
Category
Documents
-
view
224 -
download
0
Transcript of Question Bank of CPU c Programming
-
8/10/2019 Question Bank of CPU c Programming
1/80
Question Bank of CPU
SHORT QUESTIONS
Q. 1 Define Computer
Ans. A computer is an electronic data processing machine which accepts data from the
outside world in the form of an input and manipulates, calculates, computes on the
basic of a set of instructions supplied and stored in the memory and gives the
required or desired result in the form of an output to the user. To put in a simple
language, a computer is an information processing machine.
Q. 2 Define Har!are
Ans. The physical parts of a computer are called the hardware. In other words, the
units that are visible and units which one can touch and feel are known as the
computer hardware.
Q. " Define Soft!are
Ans. Software refers to the set of specific instructions that give directions to a computer
to perform certain specific or desired operations. In other words, software is
another component of a computer system which helps the user to utilize the
various capabilities of a computer system for a specific purpose.
Q. # Define $irm!are
Ans. irmware often refers to a sequence of instructions !software" that its
substituted for hardware.
Q. % Define &na'o( Computer
Ans. An analog computer is an electric system which recognizes data as continuous
measurements of physical quantities, such as pressure, voltage or temperature
along a continuous scale. The output of an analog computer will be usually in the
form of language readings or graphs.
Q. ) Define Di(ita' Computer
Ans. A #igital computer is a combination of electronic devices, designed to manipulate
physical quantities of information that are represented in a digital format. In other
words, a digital computer represents data in terms of discrete numbers andprocesses data using the standard arithmetic operations.
Q. * +,at is a ,-ri /omputer 0
Ans. $omputer which performs operations based on both analog and digital
principles in called as a hybrid computer. In other words, a computer system
that has capabilities, behavior, functions and principles of operation of both
analog and digital computer is called a hybrid computer.
%
-
8/10/2019 Question Bank of CPU c Programming
2/80
Q. i3e fu'' form of SSI4 5SI4 6SI4 76SI4 U6SI.
Ans. SSI & Small Scale integrated $ircuits
'SI & 'edium Scale Integrated $ircuits
(SI & (arge Scale integrated $ircuits)(SI & )ery (arge Scale Integrated $ircuits
*(SI & *ltra (arge Scale Integrated $ircuits
Q. 8 +,at is R&5 0
Ans. Stand for random&access memory form of primary storage for holding
temporary data and instruction volatile + rograms and data are erased when
the power is disrupted.
Q. 19 E:p'ain RO5 in rief
Ans. Stands for read&only memory form of primary storage for holding permanent data
and instructions permanent + program and data rename intact even when power is
off other forms of -' -' & rogrammable -' /-' 0 /rasable
-'.
Q. 11 +,at is a Ca/,e 5emor- 0
Ans. $ache memory, also called in high&speed buffer, is a portion of primary
storage used to speed the processing operations of the computer. $ache memory
serves as a working buffer or temporary area to store both instructions and data
that must be accessed, often by the program being e1ecuted.
Q. 12 Define Bus.
Ans.In microcomputers, the term bus refers to the paths between the components of a
computer. There are two main buses in a computer, the data bus and the address
bus.
Q. 1" Define Data Bus
Ans. The data bus is an electrical path that connects the $*, memory, and the other
hardware devices on the motherboard. Actually, the bus is group of parallel wires.
Q. 1# Define &ress Bus
Ans. The address bus is a set of wires similar to the data bus that connects the $* and
-A' and carries the memory addresses.
Q. 1% Define suroutines
Ans. Subroutines are the smallest items of software normally provided by a
manufacturer. These are usually routines devised to perform large numbers of
routine calculations e.g.,ta1 calculations multiplications and division !if these
functions are not supported by hardware" editing of data for input and output
Q. 1) Define $'o!/,art
Ans. A flowchart is a graphical representation of a program flow or an algorithm of a
problem, to be solved by a computer. It is an aid to solve a comple1 problem
2
-
8/10/2019 Question Bank of CPU c Programming
3/80
easily and efficiently.
Q. 1* Define Stati/ 7aria'es
Ans. A static can only be accessed from the function in which it was declared, like
a local variable. The static variable is not destroyed on e1it from the function,
instead its value is preserved, and becomes available again when the function is
ne1t called.
Q.1 Define Token
Ans. In a $ source program, the basic element recognized by the compiler is the
3token3. It is smallest part which does not break down further.
Q.18 +,at are ientifiers 0
Ans. 3Identifiers3 or 3symbols3 are the names you use for variables, types, functions
and labels in your program. Identifier names must differ in spelling and case
from any keywords.
Q.29 Define Es/ape se;uen/e
Ans. $haracter combinations consisting of a backslash !4" followed by a Setter or by a
combination of digits are called 3escape sequences3.
Q.21 Define Simp'e I$ statement.
Ans. The simplest form of the control statement is the If statement. It is very
frequently used in decision making and allowing the flow of program
e1ecution.
Q.22 Define If E6SE Statement.
Ans. The if else is actually 5ust on e1tension of the general format of if statement. If
the result of the condition is true, then program statement % is e1ecuted,
otherwise program statement 2 will be e1ecuted.
Q.2" Define neste I$ E6SE.
Ans. The if statement may itself contain another if statement is known as nested if
statement.
Q.2# +,at is a 'oopin( stru/ture.
Ans. 6hen user !programmer" initialize, increment a counter variable and check
!test" it value at an appropriate place in the program by repeated task such
structure is known as looping structure.
Q.2% Define Entr- Contro' 'oop.
Ans. In this loop user first checks the condition 7 if the condition becomes true it
will e1ecute the body of loop.
Q.2) Define E:it Contro' 'oop
Ans. In this loop first body of loop is e1ecuted then it checks the condition until it&
becomes true.
Q. 2* Define arra-
8
-
8/10/2019 Question Bank of CPU c Programming
4/80
Ans. Array is a group of elements which have same data types. An array is a /o''e/tion of
variables of the same type.
Q. 2 Define Strin(
Ans, String is a collection or group of character array.
Q. 28 +,at is (ets
-
8/10/2019 Question Bank of CPU c Programming
5/80
Question Bank of CPU
Q. ") +,at is str/p-< = fun/tion use for 0
Ans. Strcpyf " function assign one string value to other string va;ue in short it copy
the data from one string to another siring.
Q. "* +,at is strre3< = fun/tion use for 0Ans. Strrev!" function is used to print a string in reverse order.
Q. " Define re/ursion
Ans. -ecursion is the process where a functions is called itself but stack frame will
be&out of limit because function call will be infinite times. So a termination
condition is mandatory to a recursion.
Q. "8 Define Pointer
Ans. ointer is nothing but a variable that contains the address of another variable in
memory.
Q. #9 Define Stru/ture
Ans. A structure is a collection of variable under a single name. These variables canbe
of different types, and each has a name which is used to select it from the
structure. A structure is a convenient way of grouping
-
8/10/2019 Question Bank of CPU c Programming
6/80
Q. #) Define po'-morp,ism
Ans. olymorphism is a >reek word and its meant is to take more than one form. An
operations show different behavior in different condition. The behaviour is used on
the data type of variable and number of arguments. or e1ample following
function calls are different in terms of number of arguments. sum!int a, int b, int
c" sum!int.a, ind b" The above fundamental is known as the unction verloading.
Q. #* Define Operator O3er'oain(
Ans. 6e can perform perator verloading is one in which operator is used for multiple
operations, for e1ample 9?9 is a operator that is used for arithmetic operation the same
operator we can use for the concatation of string and add two structure elements.
Q. # Define D-nami/ Binin(
Ans. @inding means linking of two functions. There are two types of binding technique+
one is compile time binding in which linking is performed during the time of
compilation. It is also known as early binding.
Q. #8 Define 6ate Binin(
Ans. Technique of binding is dynamic binding in which linking of function is performed
during the e1ecution when function is called. It is also known as the late binding.
Q. %9 Define >e-!ors
Ans. In $?? program certain words have fi1ed mean for compiler and cannot be used for
other purpose in program. These fi1ed words are known as keywords.
Q. %1 Define Po'-morp,ism
Ans. The polymorphism can be.defined as + 3ne Interface, multiple methods.3
Q. %2 +,at is a 3irtua' fun/tion 0Ans. )irtual function is e1ample of runtime polymorphism. In virtual function we
have pointer of base class, which can point to either base class ob5ect or derive class
ob5ect depending of address of ob5ect assigned to pointer variable.
6hen we use the same function name in both the base and derived classes, the
function in base class is declared as virtual using the keyword virtual before the
function declaration.
? ? ?
-
8/10/2019 Question Bank of CPU c Programming
7/80
$REQUENT6@ &S>ED QUESTIONS
%. #efine $omputer
2. State the difference between hardware 7 software.
8. 6hat is a firmware B
:. State the advantage of computer.
=. /1plain classification of computer.
. 6rite a shortnote on Cybrid $omputer.
D. #efine !%" 'ini $omputer !2" 'icro $omputer !8" 'ain rame
!:" Super $omputer !=" Analog $omputer !" #igital $omputer.
E. /1plain @asic structure of $.
F. /1plain the difference between -A'G-'.
%H. 6hat is $ache 'emory B
%%. /1plain classification of memory with a neat diagram.
%2. State types of -'.
%8. 6hat are the points be considered while buying a S'S B
%:. 6hat do you mean by @*S. /1plain different types of @*S.
%=. 6hat are trigraph character B Cow are they useful B
%. State all types of operators.
%D. /1plain scope of variables.
%E. 6hat is a token in $ B
%F. /1plain #ata Types in #etail.
2H. #isr . 5uish break 7. continue.
2%. 6hat is a looping structure B Types of looping structure B
22. #ifferenciate while and #o while.
28. 6hat ia string B 6hich are operations on string B
2:. /1plain !a" getcharf " !b" putchar! " !c" gets!" !d" puts! ".
2=. /1plain string handling function.
D
-
8/10/2019 Question Bank of CPU c Programming
8/80
S,ort Questions
2 6hat is a functions B
2D 6rite a short note on call by value 7 call by reference.
2E 6hat is recussion B /1plain it with e1ample.
2F 6hat is a pointer B
8H #ifferenciate arrays fit pointers.
8% 6hat is a structure B
82 #ifferenciate structure 7 arrays B
88 6hat are the features of ob5ect oriented programming B
8: 6hat are benefits of s B
8= /1plain structure of $?? program.
8 6hat is a token B8D #efine eyword.
8E /1plain basic data types of $??.
8F 6hat are user defined data types B /1plain with e1ample.
:H 6hat are derived datatypes B /1plain with e1ample.
:% 6hat are free store operators B
:2 /1plain e1pression 7 their types.
:8 /1plain entry control loop 7 e1it control loop.
:: 6hat is prototyping B
:= 6hat are inline functions B
: /1plain function overloading
:D Cow to make outside function inline B
:E 6hat is a friend function B 6hat are its special characteristics.
:H 6hat is a virtual function B /1plain in detail
=H /1plain lash 'emory.
J J J
-
8/10/2019 Question Bank of CPU c Programming
9/80
TUADe/.299 Paper !it, &ns!ers
Q. 1 E:p'ain Basi/ Stru/ture of C Pro(ram
Ans. A $ rogram may conain one or more sections shown in igure
#ocumentation Section
(ink Section
#efinition Section
>lobal #eclaration Section
main! " unction Section
#eclaration art
/1ecutable art
Subprogram Section
!*ser&defined functions"
The documentation section consists of a set of comment lines giving the name of the program,
the author and other details which the programmer would like to use later, The link section
provides instructions to the compiler to link functions from the system library. The definition
section defines all symbolic constants.
There are some variables that are used in more than one function. Such variables are called
global variables and are declared in the global declaration that is outside of al; the functions./very $ program must have one main!" function section. This section contains two parts,
declaration part and e1ecutable part. The declaration part declares all the variables used in the
e1ecutable part. There is at least one statement in the e1ecutable part. These two parts must
appear between the opening and the closing braces. The program e1ecution begins at the
opening brace and ends at the closing brace. The closing brace of the main function section is
the logical end of the program. All statement in the declaration and e1ecutable part end with a
semicolon.
The subprogram section contains all the user&defined functions that are called in the main
function. *ser&defined functions are generally placed immediately after the main function,
although they may appear in any order.
All sections, e1cept the main function section may be absent when they are not required.
-
8/10/2019 Question Bank of CPU c Programming
10/80
Q. 2 E:p'ain token in C
Ans. In a passage of te1t, individual words and punctuation marks are calk tofcens.% Similarly, in a
$ program the smallest individual units are known as $ tokens. $ has; si1 types of tokens as
shown in figure. $ programs are written using these tokens and; the synta1 of the language
C TO>ENS
Q. " i3e a S!it/, /ase e:amp'e.
Ans. we write a menu driven program for simple calculator using switch.
K include Lstdio.hM
K include Lconio.hM
K include Lprocess.hM
)oid main I"
I
int a, b, c, choiceN
clrscr!"N
printf!34n /nter the value of A3"N
scanf!3Od3,asa"N
printf!34n /nter the value of @3"N
scanf!3Od3M7b"N
printf!34n %.Addition 4n 2.subraction 4n 8.'ultipication :.e1it9"N
printf!34n /nter the choice3"N
scanf!3Od3 ,7choice"N
Switch !choice"
P
-
8/10/2019 Question Bank of CPU c Programming
11/80
case %+
cQa?bN
bre
akN
case %+
cQa&bN
breakN
case 8+
cQaRbN
breakN
case :+ e1it!"N
default +
printf!34n Invalid choice3"N
breakN
printf!34n Answer is $ Od3,c"Ngetch!"N
OUTPUT
/nter the value of A %2
/nter the value of @ %8
% .Addition 2. subtraction
8.multipication :. e1it
/nter your choice % Answer is
$ 2=
Q. # E:p'ain Basi/ units of Computer.
Ans. As shown in igure 2.%, a computer system consists of four parts +
Cardware, also known simply as the computer Software, also known as
rograms #ata, which the system converts into information eople, also known
as users The term hardware refers to the part of the computer you can touch. It
consists of interconnected electronic devices that control everything the
computer does. 6hen most people talk about a computer, they mean
hardware. The term software refers to set of electronic instructions that tell the
hardware what to do. These set of instructions are also known as programs, and
each of them has a specific purpose. or e1ample, you will probably use a word
processing program to enter, edit, and format te1t documents, such as letters,
memos, and reports, 6ord erfect and 'icrosoft 6ord are two popular word
processing programs.
-
8/10/2019 Question Bank of CPU c Programming
12/80
#ata refers to the raw facts the computer can manipulate data can consist of
letters numbers, sounds, or images. Cowever, no matter what kind of data is
entered into a. computer, the computer converts it into numbers. $onsequently,
computerized data is digital, meaning it has been reduced to digits, or numbers.
6ithin the computer, data is organized into files.
$omputer file is simply a set of data or program instructions that has been given a na
A file containing data is often called a document. .Although many people think of docume
simply as te1t, a computer document can include many kinds of data. or e1ample, a comp
document can be a te1t file !such as a letter", a group of numbers !such as a budget", or a vi
clip !which includes images and sounds". rograms are organized into files as well, but becaprograms are not considered as data, these are not document files.
The last part of the computer system is the person who uses the computer. In discuss
about computers, people are u sally referred to as users.
Q. % E:p'ain T-pe /on3ersions in etai'.
Ans. $ permits mi1ing of constants and variables of different types in an e1pression, but
during evaluation it address to very strict rules of type conversion. 6e know that the
computer considers one operator at a time, involving two operands. If the operands are of
different types, the lower9 type is automatically converted to the higher9 type before theoperation proceeds. The result is of the higher type. A typical type conversion process is
illustrated in igure.
e $om uter S stem
-
8/10/2019 Question Bank of CPU c Programming
13/80
. #ifferentiate @reak )GS $ontinue
Q.* E:p'ain (oto statement
-
8/10/2019 Question Bank of CPU c Programming
14/80
-
8/10/2019 Question Bank of CPU c Programming
15/80
M /ditor
M $ompiler
M Assembler
M perating systems
In general, software can be classified into two types+ systems software and applicationssoftware.
Q. 8 State an e:p'ain t,e &3anta(e of Computer
Ans. The advantages of selecting and using computers and how and why computers
have become an important tool in the modern world are summarized and given in follow
liv points.
1) Speed
ne the main reasons for using computers in the modern world is the speed. There is no
doubt that computers are e1tremely fast, accurate and can store a large amount of data
and can e1ecute a long sequence of instructions automatically, h is only because of
computers that certain activities and operations can be accomplished in todayVs world.
Some of the e1amples for such activities are weather forecasting, control of industrial
operations, traffic and spacecraft control, reservations of tickets, etc. 6ithout uN&
computers, it is rather very difficult to take accurate decisions, predict the shortfall orW
e1cess of the production and carry out day&to&day managerial activities and functions"
associated with the social, financial, technological and other areas.
2) Storage$omputer units can store and process vast amount of information in very little
space. Also any information can be located, copied, deleted, browsed, navigated, modifier;
and displayed swiftly at the touch of a few keys. The above facility is unique to computeriven number is + Od3, 1"N
while !1 ;Q H"N
-
8/10/2019 Question Bank of CPU c Programming
43/80
iii"./1plain $o''o!in( Strin( 5anipu'ation $un/tions. 9%
Str/mp
-
8/10/2019 Question Bank of CPU c Programming
44/80
The function strstr returns a pointer to the first occurrence of str2 in strl, or *((
if no match is found.
strchr!"+
Synta1&char Rstrchr! const char
Rstr, int ch "N #escription+
The function strchr returns a pointer to the first occurence of ch in str, or *((
if ch is not found.
OR
QA" Do as Dire/t
i" Dra! low chart to do t,e sum of %H
elements read from user. H=
-
8/10/2019 Question Bank of CPU c Programming
45/80
ii= 6ist out Operators use in C 6an(ua(e an
e:p'ain an- $our !it, E:amp'e 9%
&ns
%" Arithmetic operator
2" (ogica%operator
8Y Assignment operator
:" -elational operator
=" $onditional operator
" @it&wise operator
DW Special operator
E: p'anations
Arithmetic Operator
? Addition
Subtraction
G #ivision
R 'ultiplication
O modulo
#ecrement !post and pre"
?? Increment !post and pre"
Arithmetic cperator are used for arithmetic operation.
&ssi(nment
These all perform an arithmetic operation on the lvalue and assign the result to t,elvalue.
Cere is the full set.
RQ 'ultiply GQ
#ivide. OQ
'odulus. ?Q
add.
Subtract. _Q
left shift. Q
-ight shift. 7Q@itwise A#.
bitwise e1clusive OR
-
8/10/2019 Question Bank of CPU c Programming
46/80
19#MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMTU De/. OS Paper !it, &ns!er
ogical'(elational
QQ /qual to ;Q
ot equal to
ML
M
Q
(ogical A# [ [ (ogical - ; (ogical T These
operators are used for (ogical and -elational operation.
)it*ise
7 A# !@inary operator"
[ inclusive - e1clusive- _ shift left.
shift ri
-
8/10/2019 Question Bank of CPU c Programming
47/80
Question Bank of $* %H=
Integer $onstants An integer constant is a sequence of digits. There are 8 types of
integers namely decimal integer, octal integers and he1adecimal integer.
'ntegers consists of a set of digits H to F preceded by an optional ? or & sign.
Spaces, commas and non digit characters are not permitted between digits. /1ample
for valid decimal integer constants are
%28
&8%HH
ctal'ntegers constant consists of any combination of digits from H through D with a
H at the beginning. Some e1amples of octal integers are
O2)
O)*)
4e2adecimal integer constant is preceded by J or 1, they may contain alphabets
from A to or a to f The alphabets A to refers to %H to %= in decimal digits. /1ample
of valid he1adecimal integers are
HJ2f .
O?C
Sin('e C,ara/ter Constants
A Single $haracter constant represent a single character which is enclosed in a pair of
quotation symbols.
/1ample for character constants are 9=9
All character constants have an equivalent integer value which are called AS$II
)alues.
Strin( Constants
-
8/10/2019 Question Bank of CPU c Programming
48/80
%H TU De/. OS Paper !it, &ns!er
A string constant is a set of characters enclosed in double quotation marks. The
characters in a string constant sequence may be a alphabet, number, special character
and blank space. /1ample of string constants are 3%28:3 Ko B'ess
Ba/ks'as, C,ara/ter Constants Es/ape Se;uen/es
@ackslash charactei constants are special characters used in output functions. Although
they contain two characters they represent only one character. >iven below is the,
table of escape sequence and their meanings.
$onstant 'eaning
R4_9 .Audible Alert !@ell"
94v . @ackspace
94 . ormfeed
94n9 .ew (ine
) .$arriage -eturn
94t9 .Corizontal tab
94v .)ertical Tab
) .Single uote
t5 6r .#ouble uote
94B9 .uestion 'ark
94) .@ack Slash
94H9 .ull
-
8/10/2019 Question Bank of CPU c Programming
49/80
Question Bank of CPU 9*
&:" #o as directed
i" 6hat is ointerB 6rite a program to do swapping
of two elements using pointers. H=&ns
In c a pointer is a variable that points to or references a memory location in which data
is stored. /ach memory cell in the computer has an address that can be used to access
that location so a pointer variable points to a memory location we can access and
change the contents of this memory location via the pointer.
ointer declaration+
A pointer is a variable that contains the memory location of another variable. The
synta1 is as shown udow. ou start by specifying the type of data stored in the location
identified by the pointer. The asterisk tells the compiler that you are creating a pointervariable. inally you give the name of the variable,
type R variable name
/1ample+
int RptrN float
9stringN
Address operator+
nce we declare a pointer variable we must point it to something we can do this by
assigning to the pointer the address of the variable you want to point as in the following
e1ample+
ptrQ7numN
rogram+
include Lstdio.hM void
change5int R,intR"N
int main !"
i intaQ2,bQ=N
-
8/10/2019 Question Bank of CPU c Programming
50/80
%HE TU De/. 9 Paper !it, &ns!er
printfT@efore i aQOd,bQOd4n3,a,b"N
change!7a,7b"N
printff After + aQOd,bQOd4n3,a,b"N
return HN
Y
void change!int Ra,int Rb"P
Ra ?QU RbN Rb Q Ra&RbN Ra Q
Ra&RbN I
ii= 6ist out t,e /ate(ories of fun/tions. E:p'ain
an- one /ate(or- !it, e:amp'e. 9%
&ns T-pes of fun/tions
A function may belong to any one of the following categories+
%. unctions with no arguments and no return values.
2. unctions with arguments and no return values,
8. unctions with arguments and return values.
$un/tions !it, no ar(uments an no return 3a'ues
(et us consider the following program
GR rogram to illustrate a function with no argument and no return valuesRG
tt include
rnain
i
staetemtnlN
starlineN
-
8/10/2019 Question Bank of CPU c Programming
51/80
Question Bank of $* %HF
statement2!"N
starline![N
%
G9function to print a messageRG
state mentl
5
printf!34n Sample subprogram output3"N
;
state men t2
printf!34n Sample subprogram output two3"N
starline
int aN
for !aQlNaLHNa?&t&"
printfr4n3"N
In the above e1ample there is no data transfer between the calling function and the
called function. 6hen a function has no arguments it does not receive any data from
the calling function. Similarly when it does not return value the calling function doesnot receive any data from the called function. A function that does not return any
value cannot be used in an e1pression it can be used only as independent statement.
-
8/10/2019 Question Bank of CPU c Programming
52/80
Question Bank of CPU 1
119 MMMMMMMMMMMMMMMMMMMTUMDe/. 9 Paper !it, &ns!er
iii= State ifferen/e et!een Stru/ture an Union. 9#
&ns *nion allocates the memory equal to the ma1imum memory required by the
member of the union but structure allocates the memory equal to th3 total memory
required by the members.
In union, one block is used by all the member of the union but in case of
structure, each member has their own memory space
All the members of the structure can be accessed at once, !,ere as in an union only
one member can be used at a time. Another important difference is in the size allocated
to a structure and an union.&
Stru/ture Union
& 'ore memory is occupied & 'emory allocation is 'ess
& All the members of structure & In union only one memorycan be accured at once. can be used at a time.
for e.g.+ struct
e1ample
P
int integerN float
floatingnumbersN
the size allocated here is
sizeoff in t"?sizeof !float"N
where as in an union
union e1ample
;
int integerN float
floatingnuir.bersN
I
size allocated is the size of the highest member.
so size isQsizeof!floNMt"N
OR
QA#= o as ire/te.
i= +rite a pro(ram to fin out sum of )
e'ements of arra- usin( pointer. 9%
-
8/10/2019 Question Bank of CPU c Programming
53/80
Question Bank of CPU
)oid main
Int R&Not,I,totalQHN
Int 1^WQP%H,2H,8H,:H,=H,H+N
Tot Q71PHWN
$or
-
8/10/2019 Question Bank of CPU c Programming
54/80
112 TU De/. 9 Paper !it, &ns!er
tfinclude &Qiostream.hM
/'ass myclass
stati/ int K
pu'i/
3oi setlnR!int n" % i Q
nN
%
int getlnt P
return i
%
int myclass++iN
GG #efinition of myclass++i. i is still private to myclass.
int rnain!"
{ myclass ob5ect %, ob5ect2N U
ob5ect l.setlnt! %H"N
cout _ 3ob5ectl.i+ 3 8 ob5ect l.getlnt!" _ 94n9N GG displays %H cout _
3ob5ect
-
8/10/2019 Question Bank of CPU c Programming
55/80
Question Bank of CPU 11"
return 9
iii= State t,e ifferen/e et!een entr- /ontro' 'oop
an e:it /ontro' 'oop. 9#
In general both the while loop and do&while loop are iterative control structures
in an-programming language. @oth are conditional loops because these are
based on conditions !@oolean e1pressions". The value of Boo'ean e1pression
!true 0false9 determines whether the loop e1its or. not. @ut there is a little
difference in their way of iterating. The asi/ difference is in the time of
checking the condition during e1ecution of loop. ;n while locp the condition
is /,e/ke at the start, if it is true then statements enclosed in the loop structureare e1ecuted , otherwise the loop e1its and control transfers to the statements
following this loop an this process continues until the condition becomes
false. Also is there a way to run a contro(loop in the background so that I don9t
have to enter a character to continue the loop with each entry. Initiaily I have I t
set up so that I enter 3c3 from a menu to enter /,e/ks4 and then for each
additional entry I have to say 3c3 to repeat the loop for another entry. !In other
words I wouid like to 5ust be able to enter a loop and stay there until I am done
entering the checks, and then 5ust hit 313 or whatever to e1it the loop, without
having to restart the loop with each entry."
QA% Do as Dire/te.
1= E:p'ain t,e /on/ept of /'ass4 oe/t4 po'-morp,ism an in,eritan/e. 9#
A class is a programming language construct that is used as a blueprint to
create ob5ects. This blueprint includes attributes and methods that the created ob5ects
all share.
*sually, a class represents a person, place, or thing & it is an abstraction of a
concept within a computer program. undamentally, it encapsulates the state and
behavior of that which it conceptually represents. It encapsulates state through data
placeholders called member variables7 it encapsulates behavior through reusable codecalled methods.
-
8/10/2019 Question Bank of CPU c Programming
56/80
%%: TU De/. O Paper !it, &ns!er
b5ect&oriented software is all about ob5ects. An ob5ect is a 'a/kbos3 which
receives and sends messages. A black bo1 actually contains code !sequences of computer
instructions" and data !information which the instructions operates on". Traditionally,
code and data have been kept apart.
olymorphism means any forms, it refers to the capability of ob5ects to react
differently to the same method. olymorphism can be implemented in the Xava language
in the form of multiple methods having the same name. Xava code uses a late&binding
technique to support polymorphismN the method to be invoked is decided at runtime.
>enerally speaking, ob5ects are defined in terms of classes. ou know a lot about an
ob5ect by knowing its class. /ven if you don9t know what a penny&farthing is, if % told
you it was a bicycle, you would know that it had two wheels, handle bars, and pedals.
b5ect&oriented systems take this a step further and allow classes to be defined
in terms of other classes. or e1ample, mountain bikes, racing bikes, and tandems areall different kinds of bicycles, in ob5ect&oriented terminology, mountain bikes, racing
bikes, and tandems are all subclasses of the bicycle class, Similarly, the bicycle class
is the uperclass of mountain bikes, racing bikes, and tandems.
Hierar/,- of C'asses
@icycles
'ountain @ike+ -acing @ikes Tandem @ikes
-
8/10/2019 Question Bank of CPU c Programming
57/80
-
8/10/2019 Question Bank of CPU c Programming
58/80
%% >TJX De/. E aper !it, Answer
struct J i
int aN intbN
lN
class J ob5JN
int raain 5 ob5J.a Q HN ob5J.b Q %N cout _ 3Cere are a and b+ 3 8
ob5J.a 8 3 3 8 ob5J.b _ endlN
;
The following is the output of the above e1ample+
Cere are a and b+ H %
i''= +,at is in'ine functionB /1plain !it, e1ample, H=
The point of making a function 3inline3 is to hint to the compiler that it is worth
making some form of e1tra effort to call the function faster than it would otherwise
&generally by substituting the code of the function into its caller. As well as eliminating
the need for a call and return sequence, it might allow the compiler to perform certain
optimizations between the bodies of both functions.
6hen the campiler inline&e1pands a function call, the function9s code gets
inserted into the caller9s code stream !conceptually similar to what happens with a
tfdefine macro". This can, depending on a zillion other things, improve performance,
because the optimizer can procedurally integrate the called code optimize the called
code into the caller.
Suppose that you wish to write a function in $ to compute the ma1imum of two
numbers. ne way would be to say+
int ma1!irit a, int b"
5
return !a M b B a + b"N
@ut calling a frequently&used function can be a bit slow, and so you instead use
a macro+
Kdefine ma1!a, b" !!a" - 5b9 B !a" + !b"" The e1tra
parentheses are required to handle cases like+
-
8/10/2019 Question Bank of CPU c Programming
59/80
uestion @ank of $* %%D
ma1fa Q b, c Q d"
This approach can &work pretty well. @ut it is error&prone due to the e1tra
parentheses and also because of side effects like+
ma1!a??, b??"
An alternative in $?? is to use inline functions+
inline int ma1
-
8/10/2019 Question Bank of CPU c Programming
60/80
OR
-
8/10/2019 Question Bank of CPU c Programming
61/80
%%E TU De/. OS Paper !it, &ns!er
QA% Do as Dire/te.
I= i3e t,e ifferen/e et!een oe/t oriente'an(ua(e an pro/eure oriente 'an(ua(e. 9#
ob5ect oriented language procedure oriented language
14 Oe/t riented
programming deals withthe elemental basic partsor building bio &ks of the
problem .
%. rocedural program m ing focuses onthe steps requited lo produce the de&.iredou tcom e .
2. b5ect rientation ![in general, is amethodology for modelingthe real world or at leastthe problem being solved,
by decomposing theproblem into smallerdiscrete pieces called3b5ects3.
2. rocedural rogramming, by contrast,is a methodology for modeling the realworld or the .problem being solved, bydetermining the steps and the order ofthose steps that mustbe followed in orderto reach a desired outcome or specific
program state.
8. 3b5ects9% will make upevery part of th e solution .th at is. every part of the
program will be made fromob5ects. ormally, there are
technical attributes of thatmust be pGesent for aprogram !or aprogram ming language" tobe considered b5ectriented, such asinheritance,
polymorphism,encapsulation
8 . or e1am pie. consider the problem+3calculate the month end closing balancefor an account.3 The process is take thestarting balance, subtract all the debitsand add all the credits for the period an
then you arrive closing balance. The keypoint for procedural programming isidentification and articulation of the
process or sleps that must be followed.
:. b5ect riented
programming providestools to deal with theseob5ects, such as 3a hook,3in the abstract world ofwriting software more
logically and naturally.
: .rocedural provides a way of solving theproblem or a way of thin king about tlieproblem, not the language used to writeth e codp.
=. b5ect rierted(anguages have the ben efit of su pportin g ke v aspecti,f b5ect rientation inthe language itself makingil easier to im pie n en tb5ect riented programs.
= .The key point for proceduralprogramming is identification andarticulation of the pro cess or steps thatm ust be followed
-
8/10/2019 Question Bank of CPU c Programming
62/80
Quest ion Bank of CPU 1
8
11G E:p'ain frien fun/tion !it, e:amp'e. 9%
A friend function is a non member used for accessing the private members of a
class. A class can allow non&member functions and other classes to access its own
private data, by making them friends. Thus, a friend function is an ordinary function
or a member of another class.
The friend function is written as any other normal function, e1cept the function
declaration of these functions is preceded with the keyword friend. The friend function
must have the class to which it is declared as friend passed to it in argument.
Some important pmts to note !,i'e usin( frien fun/tions in CJJ
The keyword friend is placed only in the function declaration a the friend
function and not in the function definition.
It is possible to declare a function as friend in any number of classes.
6hen a class is declared as a friend, the friend class has access to the
private data if the class that made this a friend.
A friend function, even though it is not a member function, would have the
rights to access the private members of the class.
It is possible to declare the friend function as either private or public.
The functior. can be invoked without the use of an ob5ect. The friend functionhas its argument as ob5ects, seen in e1ample below.
/1ample of mend function.
-
8/10/2019 Question Bank of CPU c Programming
63/80
1 2 9 M M T U D e / . O S P a p e r ! i t , & n s ! e r
int a,bN
public+ 3oi
test!"
a199
bQ2HHN
friend int compute!e1forsys el"
GGriend unction #eclaration with keyword friend and with the ob5ect of
class e1forsys to which it is friend passed to it
int compute,e1forsys el"
GGriend unction #efinition which has access to private data
return intle%
.a?e2.b"&=N
-
8/10/2019 Question Bank of CPU c Programming
64/80
Question Bank of CPU
121
mam!"
e1forsys eN
e.testP"N
cout
-
8/10/2019 Question Bank of CPU c Programming
65/80
TU De/. 9 Paper !it, &ns!er
+$,A(A- -E./0&0+".A& $/"%E(S"-
B.E. a'' SemAI E:amination De/emer 9Lanuar- 98
Computer rogramming an Uti'iation < 1 1999"=
QA'. Attempt following programs in $G$?? language.
-
8/10/2019 Question Bank of CPU c Programming
66/80
uestion @ank of $* E=
mul[i"(X;QHN
prinif!34n 'ultiplication of matri1 is + 4n3"N
for!iQHNiL8Ni??"
'
printf!3Od4t3,
getchf"N
1
Output
/nter irst 'atri1 + /nter
the value of a[HW^HWQ % /nter
the velue of a^H"[lWQ % /nter
the value of a^HW^2WQ % /nter
the value of a[lW^H%Q 2 /nter
the value of a!lW[lWQ2 /nter
the value of a^l[[2%Q 2 /nter
the value of a!2W^HWQ 8 /nter
the value of a^2W[ lWQ 8 /nter
the value of a^2W^2%Q 8
/nter Second 'atri1 + /nterthe value of blW^HWQ % /nter
the velue of b^HW5lWQ 2 /nter
the value of b[H"^2WQ 8
-
8/10/2019 Question Bank of CPU c Programming
67/80
E TU De/. O Paper !it, &ns!er
/nter the value of b!i W!HWQ %
/nter the value of b^lW^l5Q 2
/nter the value of b^lWP2[Q 8/nter the value of b^2W5WQ %
/nter the value of b^2WflWQ 2
/nter the value of b^2WP25Q 8
'ultiplication of matri1 is +
8 F
%2 %E
F %E 2*
-
8/10/2019 Question Bank of CPU c Programming
68/80
-
8/10/2019 Question Bank of CPU c Programming
69/80
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMTU De/. OS Paper !it, &ns!er
cout_R4nRR3RRRRRRRRR iRRRRRRRRRRRiR3_endlN
cout_34ri /nter Student I#+ 3N
/inXstuYD
cout_34n /nter Student ame+ 3N
cmstudnameN
cout&9L34n /nter Student hone o.+ 3N
cinMQMstudphnoN
cout_34n /nter Student ercentage+ 3N
cinM&studpcrcentageNI
i
void student ++ writedata!" 5
cout_R4n4nRRRRRRRRRRRRRRRRRRRRR3_endlN
cout_34n ST*#/T I-'ATI 3_endlN
cout_34nRRRRRRRRRRRRRRRRRRRRR3_endlNcout_34n Student I# + 3_studI#_endlN
cout +L34n Student ame + 3_studname_endlN
cout_34n Student hone o. + 3_studphno_endlN
cout_34n Student ercentage+ 3_studpercentage_endlN
GG 'AI*$TI
void main5" i
student siN
clrscr!"N&sl.readdata!"N
sl.wGitedata!"N
getchN
Output
/T/- ST*#/T I-'ATI
Student I# + %
Student at.ie + -a5eshStudent hone o. +F=2DH%28:=
-
8/10/2019 Question Bank of CPU c Programming
70/80
Question @ank of $* EF
Stuent ercentage+ H
RRRRRRRRRRRRRRRRRRRR;
ST*#/T I-'ATI
Student I# + %
Student ame + -a5esh Student
hone o. + F=2DH%28:= Student
ercentage+ H
2+
u #o as directed.9*
ind out the output of following $ code. Assume ali the libraries has been
included a+.d code is syntactically correct.
void mainf5
P
char cAddress5S5 Q 3>tu /1amination3N
int iJ Q D=E:N
float f Q F=.D=EN
prin tfTO.s )ri)Address"N
printf!3ODs4n3,cAddress"N
prindrOH%Hd4n3,iJ"N
printfrO%Hd4n)iJ"N
printfl9OR.Rf4n%9,D,2%f!N
printfrfceJn3, !"N
printffO%2.:e4n`, &f"N
Output
> t u / y> t u / J a m I n a t i o n
H H H H H H 2 H : E
& 2 H : EF = D D
F = D r E H / ? H %
& F = D / ? H %
-
8/10/2019 Question Bank of CPU c Programming
71/80
Question Bank of CPU89MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMTULLe/. OS Paper !it, &ns!er
ii" ind out the error if any in the following code and correct it and give the
output.
void main!"
;
intio^8W Q X%H,2H,8H,:HN+
char came5W Q 3e1am3N char
c'idd W Q 3university3N
strcpy!came, c'idd"N
printf!3Od3, io^H["N printf !3Os3,
cameYN
void mainP"
i
int i'o^:W Q ;%H,2H,8H,:H[N char
came5S" Q 3e1am3N char
c'idd l %W Q 3university3N
printfrOd3, io^HW"N
strcpy!came, c'idd"N printf
!3Os3, came"N
Output
%H ,
university
!b" Attempt any two. HD
i= 6hat is structureB Cow do we declare and access structure variablesB
Answer+
Astructure is a collection of one or more variables grouped under a single
name for easy manipulation. The variables in a structure, unlike those
in an array, can be of different variable types. A structure can contain
any of $9s data types, including arrays and other structures. /ach variablewithin a structure is called a member of the structure. The ne1t section
shows a simple e1ample.
-
8/10/2019 Question Bank of CPU c Programming
72/80
Question Bank of CPU
81
ou should r.tart with simple structures. ote that the $ language makes
no distinction between simple and comple1 structures, but it9s easier to
e1plain structures in this way.
&M #efining and #eclaring Structures
struct tag
;
structure additional statements may go here RG
YN
To use the template, you use the following format+
struct tag instance7
To use this tbrmat, you must have previously declared a structure with
the given tag.
/1ample %
GR #eclare a structure template called SS RG
struct SS
-
8/10/2019 Question Bank of CPU c Programming
73/80
F2 TU De/Z OS aper !it, &ns!er
int firstthreeN
char dashlN int
secondtwoN char
dash2N int last,
fourN
7
0 *se the structure template RG
struct SS customerssnN
E:amp'e 2
GR #eclare a structure and instance together RG
struct dat.5f
!
char month!2WN
char day^25N char
year^:WN ;
currentdateN
E:amp'e "
GR #eclare and initialize a structure RG
struct time
;
int hoursN
int minutesN
int secondsN timeofbirth Q ^ E, :=, H YN
If you9re writing a graphics program, your code needs to deal with the
coordinates of points on the screen. Screen coordinates are written as
an 1 value, giving the horizontal position, and a y value, giving the vertical
position. ou can define a structure named coord that contains both the
1 and y values of a screen location as follows+
struct coo&d
int 1N
intyN
-
8/10/2019 Question Bank of CPU c Programming
74/80
Question Bank of $*
F8
The struct keyword, which identifies the beginning of a structure
definition, must be followed immediately by the structure name, or tag
!which follows the same rules as other $ variable names". 6ithin the
braces following the structure name is a list of the structure9s member
variables. uu must give a variable type and name for each member.
The preceding statements define a structure type named coord that
contains two integer variables, 1 and y. They do not, however, actually
create any instances of the structure coord. In other words, they dont
declare !set aside storage for" any structures. There are two ways to
declare structures. ne is to follow the structure definition with a list of
one or more variable names, as is done here+
struct /oo i
int 1N
intyN
5 first, secondN
These statements define the structure type coord and declare two
structures, .Irst and second, of type coord, first and second are each
instances of type coordN first contains two integer members named 1 and
y, and so does second.
This method of declaring structures combines the declaration with the
definition. The second method is to declare structure variables at a
different location in your source code from the definition. The following
statements also declare two instances of type coord+
struct cooi9d
;int 1N
intyN
;NGR Additional code may go here RG
struct coo&d first, secondN
&M Accessing Structure 'embers
Individual structure members can be used like other variables of the
same type. Structure members are accessed using the structure member
-
8/10/2019 Question Bank of CPU c Programming
75/80
TU De/. 9 Paper !it, &ns!er
operator =.3 also called the dot operator3between the structure name and
the member name. Thus, to have the structure named first refer to a
screen location that has coordinates 1Q=H, yQ%HH, you could write
first.1 Q =HNfirst. y Q %HHN
To display, the screen locations stored in the structure second, you could
write
% printf!3Od.Od3, second.1, second.yYN
At this point, you might be wondering what the advantage is of using
structures rather than individual variables. ne ma5or advantage is that
you can copy information between structures of the same type with a
simple equa Xon statement. $ontinuing with the preceding e1ample, the
statement
first Q secondN is equivalent
to this statement+
first.1 Q second.1N
first. y Q second.yN
6hen your program uses comple1 structures with many members, this
notation can be a great time&saver. ther advantages of structures will
become apparent as you learn some advanced techniques. In generaW,
you %% find structures to be useful whenever information of different
variable types needs to be treated as a group. or e1ample, in a mailing
list database, each entry could be a structure, and each piece of
information !name, address, city, and so on" could be a structure member.
ii" 6hat do you mean by recursive functionB /1plain with small e1ample.
&ns!er
The term recursion refers to a situation in which a function c7lls itself
either directly or indirectly.'ndirect recursion occurs when one function
calls another& function that then calls the first function. $ allows recursive
functions, and they can be useful in some situations.
or e1ample, recursion can be used to calculate the factorial of a number.
The factorial of a number 1 is written 1; and is calculated as follows+
1; Q 1 R !1&%" R !1&2" R !1&8" R ... R !2" R %
-
8/10/2019 Question Bank of CPU c Programming
76/80
Question Bank of CPU
8%
Cowever, you can also calculate 1; like this+
1; Q 1R !1&l[; >oing one step further, you can calculate !1&%"; using the sameprocedure+
!1&%"; Q P1&l" R!1&2[;
ou can continue calculating recursively until you9re down to a value of
%, in which case you9re finished. The program in (isting =.= uses a
recursive function to calculate factorials. @ecause the program uses
unsigned integers, it9s limited to an input value of EN the factorial of F
and larger values are outside the allowed range for integers.
/1ample+ *sing a recursive function to calculate factorials.
GR #emonstrates function recursion. $alculates the RG
GR factorial of a number. RG
Kinclude Lstdio.hM
int f, 1N
int factorial5int a"N
void main5"
t
printff /nter an integer value between % and E+ 3"N
scanf!3Od3, 71"N ifP 1 M E [ [ 1 L %"
!printf!3nly values from &% to E are acceptable;3"N
+else
P
f Q factorial!1"N printf!3Ou factorial equals
Ou4n3, 1, f"N
I returnHN
int factorial!irt a"
P
if!aQQ %Y
return %N
-
8/10/2019 Question Bank of CPU c Programming
77/80
F TU De/. OS saper A3it, Answer
else
a RQ factorial!a&l"N
return aN
Output
/nter an integer value between % and E+
factorial equals D2H
AA(SIS+ The first half of this program is like many L the other
programs you have worked with so far. It starts with comme s on lines
% and 2. n line :, the appropriate header file is included or ic inputG
output routines. (ine declares a couple of unsigned inte< r values.
(ine D is a function prototype for the factorial function. oli +& that it
takes an unsigned int as its parameter and returns an unn ,ned int.
(ines F through 2= are the main function. (ines %% and , print a
message asking for a value from % to E and then accept an en*+.& #A value.
R & f
(ines %: through 22 show an interesting if statement. @erai U_U a value
greater than E causes a problem, this if statement checks the v. 9luc. If it9s
greater than E, an error message is printedN otherwise, the progi,. m figures
the factorial on line 2H and prints the result on line 2%. 6hen ou know
there could be a problem, such as a limit on the size of a number, add code
to detect the prohlem and prevent it.
ur recursive function, factorial;", is located on lines 2D through 8. The
value passea is assigned to a. nline 2F, the value oTa ischcck$9&+ If it9s %,
the program returns the value of %. If the value isn9t %, a is sci Nqual to
itself times the value of factorial!a&l". The program calls the factorial 3unction
again, but this time the value of a is !a&%". If !a&%" isn9t equal to %, l.&otorial!"
is called again with !!a&l"&l", which is the same as !a&2". This process
continues until the if statement on line 2F is true. If the value of the factorial
is 8, the fac9orial is evaluated to the following+
8R !8&%" R !!8&%"&%W
-
8/10/2019 Question Bank of CPU c Programming
78/80
Question Bank of $*
FD
iii" /1plain conditional operator with small e1ample.
Answer+
The statements
if !a M b"
z & aN
else
z Q bN
compute in z the ma1imum of a and b. The conditional e2pression3 written
with the ternary operator 3B+3. provides an alternate way to write this
and similar instructions. In the e1pression
f2pr"B e2pr!$ e2pr?
the e1pression e2pr7 is evaluated first. If it is non&B,ero !true", then the
e1pression e2pr.3 is evaluated, and that is the value of the conditionale1pression. therwise e2pr. is evaluated, and that is the value. nly one
of e1pr, and e2pr%is evaluated. Thus to set to the ma1imum of a and b,
D {a - b B a. $ b7 0> E ma1!a, b" RG
It should be Rioted that the conditional e1pression is indeed an e1pression,
and it can be used wherever any other e1pression can be. If e1pr, and
e1pr, are of different types, the type of the result is determined by the
conversion rules discussed earlier in this chapter. or e1ample, if f is a
float and n an int, then the e1pression
n M H" B f + n is of type float regardless
of whether n is positive.
arentheses are not necessary around the first e1pression of a conditional
e1pression, since the precedence of B+ is very low, 5ust above assignment.
They are advisable anyway, however, since they make the condition part
of the e1pression easier to see.
The conditional e1pression often leads to succinct code. or e1ample,
this loop pnnts n elements of an array, %H per line, with each column
separated by one blank, and with each line !including the last" terminated
by a newline.
for !i Q HN i L nN i??"printf!3OdOc3, a5iW, !iO%HQQF [ iQQn&l" B 94n9+ 9 R"N
-
8/10/2019 Question Bank of CPU c Programming
79/80
FE TU De/. OS Paper !it, &ns!er
& ne!'ine is printed after e3er- tenth element, and after the n&th All
other elements are followed by one blank. This might look tricky, but it9s
more compact than the equivalent if&clse. Another good e1ample is
printf!3 ou Od items Os.4n3, n, nQ&& B 33 +
3s3"+
QA" Do as Dire/t
i= Dra! a $'o! C,art to fin t,e ma:imum numer
out of t,ree (i3en Numers. 9%
ii= +,at is Pro(ram0 E:p'ain reak m Continue !it, E:amp'e 9#
&ns
A program is set of instructions and instructions are list of direction
that direct the computer to do particular task. Break statement
It is used to e1it from a loop or a switch, control passing to the first statement
beyond the loop or a switch.
&ns
-
8/10/2019 Question Bank of CPU c Programming
80/80
Question Bank of CPU FF
E:amp'e
program that tests if thegiven number is prime
number.
Kinclude Lstdio.hM