Beruflich Dokumente
Kultur Dokumente
.
Q-1) What do you mean by literals? .
Constants refer to fixed values that the program may else
not alter and they are called literals. Constants can be of { //statement(s)/else block; }
any of the basic data types and can be divided into
Integer Numerals, Floating-Point Numerals, Characters, Q-8) What are logical operators?
Strings and Boolean Values. Logical operators provide us with the capability to test
multiple conditions. C++ has 3 logical operators:
Q-2) Why do we use clrscr() function? Operator Symbol Form Operation
clrscr() is a predefined function in "conio.h" (console
input output header file) used to clear the console Logical true if x is false, or false
! !x
screen. It is a predefined function, by using this function NOT if x is true
we can clear the data from console (Monitor). Logical x && true if both x and y are
&&
AND y true, false otherwise
Q-3) What do you understand by header files?
In C and C++ Language, a header file is a file with Logical true if either x or y are
|| x || y
extension .h which contains C/C++ function declarations OR true, false otherwise
and macro definitions to be shared between several Logical NOT – It reverses the input
source files. In simple terms, it's a library file that Logical NOT (operator !)
contains various functions, macros and classes that you
can use during the development of your program. Operand Result
true false
Q-4) What is #include directive?
#include directive is used to include the header file or false true
standard file. These header files contain definition of
functions which are used in C++. Like iostream.h Logical OR – It tests whether either of two conditions is
contains declaration of cin and cout. true. If the left operand evaluates to true, or the right
operand evaluates to true, or both are true, then
Q-5) What is the difference between variable and the logical OR operator returns true. Otherwise it will
constant? return false.
The difference between variables and constants is that
Logical OR (operator ||)
variables can change their value at any time
but constants can never change their value. Left operand Right operand Result
(The constants value is locked for the duration of the
false False false
program). Constants can be very useful, Pi for instance
is a good example to declare as a constant. false True true
true False true
Q-6) What is the difference between unary and binary
operators? true True True
Binary operator requires two operands and Unary
operator requires one operand. The minus Logical AND – It tests whether both operands are true.
sign between 5 and 6 is binary operator which requires If both operands are true, logical AND returns true.
two operands (5 and 6 here). The minus sign in -11 is a Otherwise, it returns false.
unary operator. The compiler differentiates unary and
Logical AND (operator &&)
binary signs using operator precedence and
associativity. Left operand Right operand Result
false false false
Q-7) write the syntax of if else if ladder?
if(test_condition1) false true false
{ //statement(s)/block1; }
true false false
else if(test_condition2)
{ //statement(s)/block2; } true true true
Q-9) What is the difference between division (/) and grater thatn (>) , less than or equal to (<=), greater than
modulus (%) operator? equal to (>=), equivalent (==) and not equivalent (!=).
'%' is known as the modulus operator or the
remainder operator; it is used to find the remainder Logical Operators - AND (&&) and OR (||). They are
of division of two numbers. '/' is known as the division used to combine two different expressions together. If
operator; it is used to find the quotient in the division of two statement are connected using AND operator, the
two numbers. validity of both statements will be considered, but if
they are connected using OR operator, then either one
Q-10) What do you mean by escape sequence? of them must be valid.
An escape sequence is a sequence of characters
that does not represent itself when used inside a Bitwise Operators - Used to change individual bits into
character or string literal, but is translated into another a number. They work with only integral data types
character or a sequence of characters that may be like char, int and long and not with floating point values.
difficult or impossible to represent directly. E.g., ‘\n’, ‘\t’ • Bitwise AND operators &
• Bitwise OR operator |
Q-11) What do you mean by operators, write the • And bitwise XOR operator ^
names of operators supported by C++. • And, bitwise NOT operator ~
Operators are special type of functions that take one or They can be used as shorthand notation too, &
more arguments and produce a new value. For = , |= , ^= , ~= etc.
example: addition (+), substraction (-), multiplication (*)
etc, are all operators. Operators are used to perform Shift Operators - Used to shift Bits of any variable. It is
various operations on variables and constants. of three types,
1. Left Shift Operator <<
2. Right Shift Operator >>
#include<iostream.h> #include<iostream.h>
void main() void main()
{ int num; { int num;
cout<<"Hello user, Enter a number"; cout<<"Hello user, Enter a number";
cin>>num; // Collects the number from user cin>>num; // Collects the number from user
if(num==1) if(num==1)
{ {
cout<<"UNITED STATES"; cout<<"UNITED STATES";
} }
if(num==2) else if(num==2)
{ {
cout<<"SPAIN"; cout<<"SPAIN";
} }
if(num==3) else if(num==3)
{ {
cout<<”INDIA"; cout<<"INDIA";
}} }
else
The If else statement - {
if(expression 1) cout<<"WRONG ENTRY";
// Expression 1 is evaluated. If TRUE, statements inside // See how else is used to output "WRONG ENTRY"
the curly braces are executed. }}
{ //If FALSE program control is transferred to
immediate else if statement. switch statement - switch is a multi branching control
statement 1; statement.
statement 2; } switch(expression)
else if(expression 2) // Expression is evaluated. The outcome of the
// If expression 1 is FALSE, expression 2 is evaluated. expression should be an integer or a character constant
{ statement 1; {
statement 2; } case value1: // case is the keyword used to match the
else if(expression 3) integer/character constant from expression.
// If expression 2 is FALSE, expression 3 is evaluated //value1, value2 ... are different possible values that can
{ statement 1; come in expression
statement 2; } statement 1;
else // If all expressions (1, 2 and 3) are FALSE, the statement 2;
statements that follow this else (inside curly braces) is break; // break is a keyword used to break the program
executed. control from switch block.
{ statement 1; case value2:
statement 2; } statement 1;
other statements; statement 2;
break;
The execution begins by evaluation expression 1. If it default: // default is a keyword used to execute a set of
is TRUE, then statements inside the immediate curly statements inside switch, if no case values match the
braces is evaluated. If it is FALSE, program control is expression value.
transferred directly to immediate else if statement. statement 1;
Here expression 2 is evaluated for TRUE or FALSE. The statement 2;
process continues. If all expressions inside the different break;
if and else if statements are FALSE, then the }
last else statement (without any expression) is executed
Execution of switch statement begins by evaluating the Loop structures
expression inside the switch keyword brackets. The
expression should be an integer (1, 2, 100, 57 etc ) or a
character constant like ‘a’, ‘b’ etc. This expression’s
value is then matched with each case values. There can
be any number of case values inside a switch
statements block. If first case value is not matched with
the expression value, program control moves to next
case value and so on. When a case value matches with
expression value, the statements that belong to a
particular case value are executed.
Notice that last set of lines that begins with default. The
word default is a keyword in C/C++. When used inside
switch block, it is intended to execute a set of
statements, if no case values matches with expression
value. So if no case values are matched with expression
value, the set of statements that follow default: will get
executed.
Q-28) Where do we use insertion (<<) and extraction Q-34) Underline the error (if any) in the following
(>>) operators? Give an example of each. codes and rewrite correct code (consider header files
The insertion operator << is the one we usually use for and getch() in each codes).
output, as in, cout << "This is output" << endl; a) void main()
It gets its name from the idea of inserting data into the { int a;
output stream. cout<<enter the value”;
The extraction operator >> is the one we usually use for cin>>”a”;
input, as in, cin >> X; It gets its name from the idea of cout<<the value is<<a; }
extracting data from the input stream.
Correction:
Q-29) While using loops, why is it important to put void main()
break statement inside if condition? { int a;
Using break statement we can come out of the loop cout<<”enter the value”;
instantly. Whenever a break statement is encountered cin>>a;
inside a loop in if condition, the control directly comes cout<<”the value is”<<a; }
out of loop and the loop gets terminated for rest of the
iterations. It is used along with if statement, whenever b) void main()
used inside loop so that the loop gets terminated for a { inta=1,b=2,c;
particular condition. c=a+b+,
cout<<c }
Q-30) Convert the following expressions into short
hand expression: Correction:
1. a=a+(c/b) a+=(c/b) void main()
2. c=c-(a+b/d) c-=(a+b/d) {
int a=1,b=2,c=0;
Q-31) Write the difference between entry control loop c=a+b;
and exit control loop? cout<<c; }
For loop and while loop are the examples of entry
controlled loop. Here if test condition is true then c) void main()
loop body will be executed. Do while loop is the { float a=6.0,b=3.0,c;
example of exit controlled loop. Exit controlled loop is c=a\b;
used when condition can be checked at the last after cout<<c; }
executing the loop body at least once.
Correction:
Q-32) Explain the use of assignment operator (=) and void main()
relational operator == with an example of each. { float a=6.0,b=3.0,c;
= operator - The “=” is an assignment operator and is c=a/b;
used to assign the value on the right to the variable on cout<<c; }
the left. For example: a = 10; b = 20; ch = 'y';
The ‘==’ operator checks whether the two given d) void main
operands are equal or not. If so, it returns true. { int x=4,y=5z;
Otherwise it returns false. For example: 5==5, This will z=((y>x)(x<y))&&(x==y));
return true. cout<<z; }
Correction: Correction:
void main void main()
{ int x=4,y=5,z; { int a=20;
z=((y>x)||(x<y)&&(x==y)); while(a>=11)
cout<<z; } {
cout<<a;
e) void main() a--;
{ int a,b; }}
cout<<”enter the value of a &b”;
cin>>a>>b; i) void main()
c=a/4+b+1, { int a= 10;
cout<<c; } do while(a<=10)
{
Correction: cout<<a;
void main() a--;
{ int a,b; }}
cout<<”enter the value of a &b”;
cin>>a>>b; Correction:
c=a/4+b+1; void main()
cout<<c; } { int a= 10;
do
f) void main() {
{ int a=1,b=2; cout<<a;
a=+b; a--;
cout<<a; } } while(a<=10); }
Q-42) Write a program to find largest among three Q-45) Write a program to print table of a number.
numbers using nested if else #include<iostream.h>
#include<iostream.h> void main()
void main() { int i,n;
{ int num1, num2, num3; cout<<"Enter any number:";
cout << "Enter three numbers: "<<endl; cin>>n;
cin >> num1 >> num2 >> num3; for(i=1;i<=10;++i)
if(num1 >= num2) cout<<"\n"<<n<<" * "<<i<<" = "<<n*i; }
{
if(num1 >= num3) Q-46) Write a program to print the following pattern.
cout <<num1 << " is the largest number"; *
else **
cout <<num3 << " is the largest number"; ***
} ****
else if(num2 >= num3) #include<iostream.h>
cout <<num2 << " is the largest number"; void main()
else { int i,j;
cout <<num3 << " is the largest number"; } for(i=1;i<=4;++i)
{
Q-43) Write a program to check whether a candidate is for(j=1;i<=i;++j)
eligible to vote or not? {
#include<iostream.h> cout<<" * ";
#include<string.h> }
void main() cout<<”\n”; } }
Q-47) Write a program in C++ to find the electricity bill default: cout<<"Invalid input! Please enter week no.
of a person who consumes 200 units of energy and between 1-7.";
each unit is of Rs.0.75. If the total cost is more than }}
100 then 10% surcharge is added
#include <iostream.h> Q-50) Write a program to print series of odd numbers
#include <conio.h> upto nth term.
void main() #include<iostream.h>
{ clrscr(); void main()
float unit=200; { int i,n;
tc=unit*0.75; cout<<"Enter any number:";
float surcharge=0; cin>>n;
if(tc>100) for(i=1;i<=n;++i)
surcharge=tc*0.10; {
float total_cost; if(n%2!=0)
total_cost = surcharge + tc; {
cout<<"\n\nYOUR BILL AMOUNT IS "<<total_cost; cout<<n<<’\t’;
getch(); } }}}
Q-48) Write a program to swap two numbers without Q-51) Write a program to print the following pattern.
using third variable. 1
#include <iostream.h> 12
void main() 123
{ int a=5, b=10; 1234
cout<<"Before swap a= "<<a<<" b= "<<b<<endl; #include<iostream.h>
a=a*b; //a=50 (5*10) void main()
b=a/b; //b=5 (50/10) { int i,j;
a=a/b; //a=10 (50/5) for(i=1;i<=4;++i)
cout<<"After swap a= "<<a<<" b= "<<b<<endl; } {
for(j=1;i<=i;++j)
Q-49) Write a program to enter a number and print {
name of day using switch statement. cout<<j;
#include <iostream.h> }
void main() cout<<”\n”;
{ int daynumber; }}
cout<<"Enter day number(1-7): ";
cin>>daynumber; Q-52) Write a program to print the following series 1
switch(daynumber) 27 125 343 729.
{ # include<iostream.h>
case 1: cout<<"Monday"; #include<conio.h>
break; void main()
case 2: cout<<"Tuesday"; { int i=0;
break; for( i=1; i<=9;i+=2)
case 3: cout<<"Wednesday"; {
break; cout<<i*i*i<<"\t";
case 4: cout<<"Thursday"; }
break; getch(); }
case 5: cout<<"Friday";
break; Q-53) Write a program to print even numbers between
case 6: cout<<"Saturday"; I and 50 using continue statement.
break; #include<iostream.h>
case 7: cout<<"Sunday"; void main()
break; { int i;
for(i=1;i<=50;i++)
{
if(i%2==0)
{
cout<<i;
}
continue;
}
getch(); }