Professional Documents
Culture Documents
Notes Xii Dec 2020
Notes Xii Dec 2020
CLASS XII
STUDY NOTES
Prepared By:
3. What is thread?
Ans: A thread is the basic unit of CPU utilization/allocation. It is a sequence of instructions within a
process. A process is divided into multiple threads for efficient execution.
12. Differentiate between multiprogramming and multithreading by giving one example of each.
Ans:
Multiprogramming Multithreading
It loads multiple programs but runs one at a It divides a program into threads and executes
time multiple threads at a time
It is slower It is faster
Programs do not affect other programs Threads may affect the working of other
threads
Programs are independent of each other Threads are dependent on each other
Programs do not share common memory Threads share common memory locations
It is manage by OS It can be managed by programmer (user level
threads)
Old System
New System
Planning
Feasibility
Study
Analysis
Requirement
Engineering
Design
Coding
Testing
Implementation
Maintenance
8. How cursor is moved to the next tabular position for printing data?
(a) By using reserved word (b) By using manipulator
(c) By using escape sequence (d) By using header file
14. The instructions that are given to the compiler before the beginning of actual program are:
(a) C statements (b) Linking instructions (c) Preprocessor Directives (d) C instructions
17. Which of the following escape sequence is used to erase a character left to the cursor position?
(a) \b (b) \f (c) \del (d) \c
21. The processing of converting source code into object code is called ________
(a) Debugging (b) Compiling (c) Linking (d) None of these
23. What happens when the result of calculation exceeds the capacity of datatype?
(a) Syntax error (b) Logical error (c) overflow (d) math error
3. State whether the following variable names are valid or invalid. State the reasons for invalid
variable names.
Ans:
Variable Validity Reason for invalid name
Name
a123 Valid
_abcd Valid
5hml Invalid Variable name cannot start with a number
tot.al Invalid Special character like “.” Are not allowed
f3ss1 Valid
c$avg Invalid Special character like “$” Are not allowed
net_weight Valid
cout Invalid Variable name cannot be a keyword
max marks Invalid Spaces are not allowed in a variable name.
Noman Valid
fG_456 Valid
0519280605 Invalid Variable name cannot start with a number
4. Why escape sequences are used? Give three examples with explanation.
Ans: Escape sequences are the characters used in cout statement for adding special effects in output.
The control character which cannot be typed through keyboard are expressed as escape sequence.
An escape sequence consists of two characters i.e. \ followed by any character. Some of the escape
sequences are given below
Escape Sequence Purpose
\a Alert- Produces an alert sound (beep)
\b Backspace- Move cursor one character backward (to the left)
\n New Line- Issues a new line
\r Carriage Return- Moves cursor to the beginning of line.
\t Tab- Moves cursor to the next horizontal tabular position
\\ Back Slash- Writes a \
\’ Single quote- Writes a ‘
\” Double quote- Writes a “
8. Evaluate the following expressions that have integer and floating-point data types.
Ans: a. 10.0+15/2+4.3 b. 10.0+15.0/2+4.3 c. 4/6*3.0+6
10.0+15/2+4.3 10.0+15.0/2+4.3 4/6*3.0+6
10.0+7.5+4.3 10.0+7.5+4.3 0.667*3.0+6
17.5+4.3 17.5+4.3 1.999+6
21.8 Ans 21.8 Ans 7.99999 Ans
No of
Data Type Range Description
Bytes
1 character out of 255 of ASCII
Char 1 Stores character data
table
Int 4 -2147483648 to +2147483648 Stores numeric data of integer type
unsigned int 4 0 to 4294967295 Stores numeric data of integer type without sign
short int 2 -3276 to +32767 Stores numeric data of integer type
unsigned short int 2 0 to 65535 Stores numeric data of integer type without sign
long int 4 -2147483648 to +2147483648 Stores numeric data of integer type
unsigned long int 4 0 to 4294967295 Stores numeric data of integer type without sign
Stores numeric data of decimal type with 6 digits of
Float 4 -3.4-38 to 3.4-38
precision
Stores numeric data of decimal type with 15 digits
Double 8 -1.7-308 to 1.7-308
of precision
cout<<a 92 cout<<setw(4)<<a 92
cout<<b 6 cout<<setw(4)<<b 6
cout<<c 102 cout<<setw(4)<<c 102
19. What do you mean by precedence of operators? Show the precedence of all operators used in
C++.
Ans: Precedence of operator means the order in which operators are used. It determines which
operator will be used first. Following is the order of precedence of all operators.
Precedence Operator Description
1 * , / ,% Arithmetic Operator (High priority)
2 +,- Arithmetic Operator (Low priority)
3 < , <= , > , >= Relational Operator of Less than and Greater than etc.
4 == , != Relational Operator of Equal to and Not Equal to
5 ! Logical Operator NOT
6 && Logical Operator AND
7 || Logical Operator OR
8 =, *=, /=, %=, +=, -= Assignment operators
21. What are comments? Define their types. (Types to be defined if it is a part of long question)
Ans: Comments are the non-executable statements which are ignored by the compiler during
translation. These are used by the programmer for enhancing the readability of code however
computer does not use these statements. There are two types of comments as given below.
Single Line Comments ( // ): These comments convert the text into comments up to the end of line.
E.g. cout<<”\n I LOVE PAKISTAN”; // prints a message
In this line the text after // is a comment.
Multiple Line Comments ( /* ….. */ ): These comments convert the text into comments which is
between /* and */. This text may be a part of line or may be multiple lines.
E.g. cout<<”\n I LOVE PAKISTAN”; /* prints a message
And is an example of multiple line comments*/
In this line the text between /* and */ is a comment.
30. Define pre increment/decrement and post increment / decrement with the help of example.
Ans: Increment / Decrement operators are unary operators used to increase / decrease the value of an
integer variable by 1. These may be used in two forms i.e. Prefix or Postfix.
Prefix Increment: The ++ operator is written before variable. Let n is a variable. And n=5. If we use
a = ++n, then it will first apply ++ then will use the new value of n, hence 6 will be moved to a.
Postfix Increment: The ++ operator is written after the variable. Let n is a variable. And n=5. If we
use a = n++, then it will first move the current value to a then will apply ++ to n, hence 5 will be
moved to a and new value of n will be 6.
38. Write a program that reads temperature in Fahrenheit and prints its equivalent temperature in
Celsius. Using the formula F=9/5 C+32
#include<iostream.h>
#include<conio.h>
void main (void)
{
float C, F;
clrscr();
cout<<”\n Enter temperature in Celsius…”;
cin >>C;
F=9/5 * C+32;
cout<<”\n”<<C <<”Celsius = ” <<F <<”Fahrenheit”;
getch();
}
39. Write a program that inputs maximum and obtained marks of a student and find his percentage.
#include<iostream.h>
#include<conio.h>
void main (void)
{
int obt_mrx, max_mrx;
float per;
clrscr();
cout<<”\n Enter Obtained Marks…”;
cin >>obt_mrx;
cout<<”\n Enter Maximum Marks…”;
cin >>max_mrx;
per = obt_mrx / max_mrx * 100;
cout<<”\n Your Percentage is ”<<per <<”%”;
getch();
}
#include<iostream.h>
#include<conio.h>
void main (void)
{
int b_pay, ca;
float hr, med, net;
clrscr();
cout<<”\n Enter Basic Pay…”;
cin >>b_pay;
hr = b_pay * 40 / 100;
med = b_pay * 15 / 100;
ca = 5000;
net = b_pay + hr + med + ca;
cout<<”\n Net Pay = Rs. ” <<net;
getch();
}
45. Write a program to input distance in miles and convert into km (where 1mile = 1.6km).
#include<iostream.h>
#include<conio.h>
void main (void)
{
int m;
float km;
clrscr();
cout<<”Enter distance in miles….”;
cin>>m;
km = m * 1.6;
cout<<”\n “<<m <<” Miles = “ <<km <<” Kilometers”;
getch();
}
46. Write a program to input measurement of side of a cuboid and find its volume.
#include<iostream.h>
#include<conio.h>
void main (void)
{
int s, v;
clrscr();
cout<<”Enter measurement of a side of cuboid….”;
cin>>s;
v = s * s * s;
cout<<”\n Volume of given cuboid is “<<v;
getch();
}
47. Define main() function.
Ans: main() function is the building block of C-program which makes it executable. It is the first section
to be executed. Every executable code of C language must have main() function. Its definition is
surrounded by braces, { and }.
Practice all the programs given in book as examples and in practical notebook)
23. Define exit() function. OR What is the use of exit() function in C++ programs.
Ans: exit() is used to terminate a C++ program before its normal termination and exit to operating
system. It is defined in stdlib.h header file. Its syntax is: exit(value);
Value may be 0 or 1. 0 is used to exit the program without error and 1 to exit with error.
24. What do you mean by nested loop? Give one example.
Ans: Loop inside another loop is called nested loop. For every iteration of outer loop, inner loop runs its
all iterations.
Example: for(int i = 1; i <= 5; i++) OUTPUT
{
1 2 3 4 5
for(int j = 1; j <= 5; j++)
1 2 3 4 5
cout<<j;
1 2 3 4 5
cout<<”\n”;
1 2 3 4 5
}
1 2 3 4 5
25. Write the following code using while loop:
sum=0;
for(k=20; k<100; k=k+2)
sum = sum + k;
cout<<”\n Sum = “<<sum;
Ans: sum=0;
k=20;
while(k<100)
{
sum = sum + k;
k=k+2;
}
cout<<”\n Sum = “<<sum;
31. Write a program to input a number and print its table using for loop.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int n, i;
clrscr();
cout<<”Enter a number….”;
cin>>n;
for(i =1; i <= 10; i++)
cout<<”\n “<<n <<” x “ <<i <<” = “ << n*i;
getch();
return 0;
}
33. Write a program to input a character and find whether it is upper-case or lower-case.
#include<iostream.h>
#include<conio.h>
int main ( )
{
char ch;
clrscr();
cout<<”Enter an alphabet….”;
ch=getche();
if(ch >= ‘a’ && ch <= ‘z’)
cout<<”\n it is a LOWER CASE letter”;
else if(ch >= ‘A’ && ch <= ‘Z’)
cout<<”\n it is an UPPER CASE letter”;
else
cout<<”\n It is not an alphabet”;
getch();
return 0;
}
49. Write a program to print the sum of following sequence using while loop.
Sum = 30 + 33 + 36 + 39 + . . . . . . . . . + 60
#include<iostream.h>
#include<conio.h>
int main ( )
{
int i, sum=0;
clrscr();
i =3;
while (i <= 60)
{
sum = sum + i;
i= i+3;
}
cout<<”\n Sum = “ << sum;
getch();
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main ( )
{
int a, b, c, x1, x2;
float d;
clrscr();
cout<<\n Enter the coefficients a, b and c…….”;
cin >>a >>b >>c;
d = b*b -4*a*c;
if(d= = 0)
{
x1= -b / (2*a);
cout<<”\n x = { “ <<x1 << “ } “; // e.g. output will x= {2}
}
else if(d > 0)
{
x1= (-b + sqrt(d) ) / (2*a);
x2= (-b - sqrt(d) ) / (2*a);
cout<<”\n x = { “ <<x1 << “ , “ <<x2 <<” } “; // e.g. output will x= {2,3}
}
else
cout<<”\n NO REAL SOLUTION”;
getch();
return 0;
}
52. Write a program to print square of * using loop.
#include<iostream.h>
#include<conio.h>
int main ( ) OUTPUT
{ **********
int i; **********
clrscr(); **********
for(i =1; i <= 5; i= i++) **********
cout<<”**********\n”`; **********
getch();
return 0;
}
(Practice all the other programs given in book as examples and in practical notebook)
4. Which of the following identifies the first element in array named temp?
(a) temp(0) (b) temp(1) (c) temp[0] (d) temp[1]
5. Which of the following identifies the last element in array declared as int a[10][10];
(a) a[9][9] (b) a[9][10] (c) a[10][10] (d) a[11][11]
6. Given the following: int k=[3][5]= {{3,10,12,27,12),
{21,20,18,25,1},
{15,16,17,44,4}} What is in k[1][3]?
(a) 12 (b) 18 (c) 25 d) 15
7. Given the following: int arr=[3][5]= {{12,0,5,10),
{7,8,19,30},
{33,1,2,22}} Which of the following will replace 19 with 50?
(a) arr[2][3]=50 (b) arr[1][2]=50 (c) arr[2][1]=50 (d) arr[19]=50
8. Which of the following functions is used to append a string onto the end of another string.
(a) strcpy() (b) strapp() (c) strcat() (d) strcmp()
9. Which of the following functions returns a numeric value?
(a) cin.get() (b) strcat() (c) strcmp() (d) strcpy()
10. Which of the following functions inputs a string from user?
(a) cin.get() (b) getstr() (c) in.str() (d) strcpy()
11. If string1 and string2 are same than strcmp() returns____.
(a) -1 (b) 0 (c) 1 (d) “same”
12. Which of the following is used to find the number of characters in a string?
(a) cin.get() (b) strlen() (c) strcmp() (d) strcpy()
13. Which of the following is used to initialize a string?
(a) cin.get() (b) strcpy() (c) Both a and b (d) None of these
14. sizeof() function is used to find the______.
(a) number of elements of an array (b) number of bytes occupied in memory
(c) highest index of an array (d) size of program
15. For the array int a[4], sizeof(a) will return ______
(a) 3 (b) 4 (c) 8 (d) 16
ANSWER KEY: 1.D-2.B-3.C-4.C-5.A-6.C-7.B-8.C-9.C-10.A-11.B-12.B-13.C-14.B-15.D
14. Write a program that reads ten numbers in an array and prints the smallest number with index.
#include<iostream.h>
#include<conio.h>
int main ( )
{
int i, min, ind, arr[10];
clrscr();
cout<<”\n Enter 10 numbers into the array…”<<endl;
for(i=0; i<10; i++)
cin>>arr[i];
min= arr[0]; ind = 0;
for(i=0; i<10; i++)
if(arr[i] < min)
{
min = arr[i];
ind = i;
}
cout<<”\n Smallest Number is “<<c <<” at index number ”<<ind;
getch();
return 0;
}
22. Write a program that reads 3 strings and prints the smallest.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main (void)
{
char s1[20], s2[20], s3[20;
int a,b,c;
clrscr();
cout<<”\n Enter first string….”;
cin.get(s1,20);
cout<<”\n Enter second string ….”;
cin.get(s2,20);
cout<<”\n Enter third string ….”;
cin.get(s3,20);
a=strcmp(s1,s2);
b=strcmp(s1,s3);
c=strcmp(s2,s3);
if(a<0 && b<0)
cout<<”\n Smallest String is” <<s1;
else if(a>0 && c<0)
cout<<”\n Smallest String is” <<s2;
else if(b>0 && c>0)
cout<<”\n Smallest String is” <<s3;
getch();
}
clrscr();
cout<<”Enter a string…\n\n”;
cin.get(str,50);
ln=strlen(str);
getch();
}
clrscr();
cout<<”Enter a string…\n\n”;
cin.get(str,50);
ln=strlen(str);
getch();
}
(Practice all the other programs given in book as examples and in practical notebook)
#include<iostream.h>
#include<conio.h>
void mean (int a, int b, int c);
int main ( )
{
int x, y, z;
clrscr();
cout<<”\n Enter three numbers…”;
cin>>x >>y >>z;
mean(x,y,z);
getch();
return 0;
}
void mean (int a, int b, int c)
{
float m;
m = (a+b+c)/3;
cout<< “Arithmetic mean of these numbers is ”<<m;
}
18. Write a program to input the length and width of a rectangle and calculate its are using a UDF.
Function must return the value.
#include<iostream.h>
#include<conio.h>
int area (int l, int w);
int main ( )
{
int L, W, A;
clrscr();
cout<<”\n Enter length and width of rectangle…”;
cin>>L >>W;
A=area(L,W);
cout<<”\n Area of given rectangle is “<<A;
getch();
return 0;
}
int area (int l, int w);
{
int a;
a = l*w;
return a;
}
(Practice all the other programs given in book as examples and in practical notebook)
3. Explain the terms CLASS and OBJECT with example. OR Write a note on a. Class b. Object (Long)
Ans: Class: A class is a user defined data type having its own data and functions. It is the most important
feature of C++ which leads to object oriented programming. It is declared before main function.
The body of class contains members and access specifiers. The data variables inside a class body
are called Data Members and functions in it are called Member Functions. Members of the class
are defined with a access specifier which determine their accessibility. Access specifier may be
public (accessible in class itself, derived classes and main function), protected (accessible in class
itself and derived classes), private (accessible with in the class only)
Syntax to declare a class is: class ABC
{
Access Specifier 1:
Members
Access Specifier 2:
members
};
Example: class SUM
{
private:
int a,b;
public:
void Cal (void)
{
cout<<”\n Enter two numbers…”;
cin>>a >>b;
cout<<”\n Sum of “<<a<<” and “<<b<<” is “<<a+b;
}
};
public:
void Cal (void)
{
cout<<”\n Enter two numbers…”;
cin>>a >>b;
cout<<”\n Sum of “<<a<<” and “<<b<<” is “<<a+b;
}
};
Void main (void)
{
SUM C; // Declaration of object of class SUM
C.Cal(); // Accessing Member Function of class through Object
getch();
}
11. Define the levels of hiding members of a class in the form of a table.
public:
void GetRadius (void)
{
void main (void)
cout<<”\n Enter Radius of Circle…..”;
{
cin>>r;
Circle c;
}
clrscr();
void CalArea (void)
c.GetRadius();
{
c.CalArea();
cout<<”\n Area of Circle is ”<<3.142 * r * r;
getch();
}
}
};
19. Write a program to read text form file character by character and count total number of
characters read.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main (void)
{
ifstream fptr;
int c=0;
char ch;
fptr.open(“MyText.txt”);
if(!fptr)
cout<”\n File not found….”;
else
{
while (!fptr.eof())
{
fptr.get(ch);
cout<<ch;
c++;
}
fptr.close();
cout<<”\n File has “<<c <<” characters”;
}
getch();
}
﴾﴾ ꙮ G D L U C K ꙮ﴿﴿
81 Computer Science XII (By: Prof. Noman Qadeer)