Vous êtes sur la page 1sur 31

# A.

## CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-1)

Q1: Write a simple program to print your name, class and school.
#include <iostream.h>

int main()
{
cout << "My name is Akshay Kumar" << endl;
cout << "Class: XI-A (XYZ School)." << endl;
return 0;
}

Q2: Write a program to input a number (say 31) and display it in hexadecimal form (e.g.
0f).
#include <iostream.h>
int main()
{
int x;
cout << Enter an integer:" << endl;
cin >> x;
// hex keyword displays a number in hexadecimal form.
cout << "x=" << hex << x << endl;
cin.get();
return 0;
}

Q3: Write a C++ program two swap to integers without taking a temporary variable.

#include <iostream.h>
int main()
{
int a = 10;
int b = 20;
cout << "Value of a (before swap): " << a << endl;
cout << "Value of b (before swap): " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "Value of a (after swap): " << a << endl;
cout << "Value of b (after swap): " << b << endl;
cin.get();
return 0;
}

Q4:Write a program to check is a number is multiple of three or not.

#include <iostream.h>
int main()
{
int x;
cout << Enter an integer:";
cin >> x;
// use % operator to check remainder
if (x % 3)
cout << x << " is NOT a multiple of three." << endl;
else
cout << x << " is multiple of three." << endl;
cin.get();
return 0;
}

Q5: Write a program to check if a year is a leap year or not.

// program to check if it is a leap year or not
// Note: leap years occur in years exactly divisible by four,
// except that years ending in 00 are leap years
// only if they are divisible by 400.
#include <iostream.h>
#include <conio.h>

int main()
{
int year;
cout << "Enter a year (e.g. 2004): ";
cin >> year;
if ((year % 400 == 0) || ((year %100 != 0) && (year % 4 == 0)))
cout << "The year " << year << " is a leap year" << endl;
else
cout << "The year " << year << " is NOT a leap year" << endl;
getch();
return 0;
}

B. CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-2)

Q1. What is the size of a variable?

Answer: When you declare a variable, memory is allocated for that variable. For example char variable
takes 1 byte of memory while an int variable consumes two bytes.

Note how much int will occupy memory space is platform dependent. For example, Turbo C++ is DOS
based program allocates 2 bytes to an int variable. While visual c++ 6.0 compiler meant for 32-bit
Windows applications, allocates 4 bytes to an int variable.

You can check the size of a variable allocated by a compiler by using sizeof operator.
#include <iostream.h>
#include <conio.h>

int main()
{
clrscr(); // clear the screen
// using sizeof opeator to find the size of variable
cout << "size of char: " << sizeof(char) << " bytes." << endl;
cout << "size of int: " << sizeof(int) << " bytes." << endl;
cout << "size of long: " << sizeof(long) << " bytes." << endl;
cout << "size of float: " << sizeof(float) << " bytes." << endl;
cout << "size of short: " << sizeof(short) << " bytes." << endl;
cout << "size of double: " << sizeof(double) << " bytes." << endl;
int x =10;
cout << "size of variable x: " << sizeof(x) << " bytes." << endl;
float b = -200.10;
cout << "size of variable b: " << sizeof(b) << " bytes." << endl;
getch();
return 0;
}
When run (compiled with Turbo C++ 3.0), the program will give the following output:
size of char: 1 bytes.
size of int: 2 bytes.
size of long: 4 bytes.
size of float: 4 bytes.
size of short: 2 bytes.
size of double: 8 bytes.
size of variable x: 2 bytes.
size of variable b: 4 bytes.

Q2: In Turbo C++ (TC) An unsigned integer can have maximum value of 65535. What will happen if
you add another number to it?

Answer: In general, we say data overflow happens here. But the behaviour is same as seen in car
odometer. When the unsigned int crosses its maximum value, it wraps around and reset to zero.
e.g. 65535 + 1 will become 0, 65535 + 2 will become 1 and so on...
You may try the following program:
#include <iostream.h>
#include <conio.h>

int main()
{
clrscr(); // clear the screen
int val = 65535;
cout << "val = " << val << endl;
val = val + 1;
cout << "val = " << val << endl;
getch();
return 0;
}
Q3: Consider the following C++ snippet:
int x = 25000;
int y = 2*x;
cout<< y << "\t" << x;

Why does the output does not come as 50000? What could be the reason?

Answer: The range of int (compiler takes int as signed int) is from -32768 to +32767. After multiplying
the value exceeds the upper bound (+32767) and result is not the desired output. We should consider

Q4:Write a program to input a digit and display its ASCII code.

Answer: C++ gives char variable special treatment. It displays the ASCII character. E.g. if char variable
stores 65, it will be displayed as 'A'.

You may assign a character to int (e.g. int x = 'P'). When you cout << x; it will print 80 not 'P'.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// program to input a alphabet or digit
// and display its ASCII code

#include <iostream.h>
#include <conio.h>

void main()
{
char ch;
clrscr(); // clear screen
cout << "Enter a character or digit:";
cin >> ch;
int x = ch; // stores 1 byte of character as integer
cout << "ASCII code of character("<< ch << ") is: " << x << endl;
x = 'P';
cout << "x = " << x << endl;
getch();
}

Q5: What are different escape sequences or escape characters in C++?

Answer: Any character preceded by a backslash, \, is called an escape sequence, or escape character. In
general, escape characters are used to print special characters. Here is the list of escape characters:
Escape Sequence Meaning
\a Terminal Bell
\b Backspace
\f Form Feed (Used in printers)
\t Tab space (equal to four spaces)
\n New Line
\r Carriage Return
\v Vertical Tab
\\ Backslash
\' Terminal Bell
\" Terminal Bell
\? Terminal Bell
\000 Octal Number
\0 Null character

Following snippet shows the use of escape sequence.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
#include <conio.h>

void main()
{
// following line will print Hello C++ is fun.
cout << "Hello C++ is gun\b\b\bfun"<< endl;

// following uses tab spaces.
cout << "\'C++\'\tis\tfun" << endl;

// you may hear computer beep.
cout << "\a";
getch();
}

The output on screen will be:
Hello C++ is fun
'C++' is fun.

Q6: Why the Salesmen didn't get any commission?
A business man gives 3% commission to his salesman on sales done by that sales man. He asked
the programmer to write a C++ program to compute commission. To his surprise, none of the
salesman got any commission. What's wrong with following snippet?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream.h>
#include <conio.h>

void main()
{
// define float variables
float sales, comm;
cout << "Enter the sales value:";
cin >> sales;

// compute the commission
comm = (3/100)* sales;
// following uses tab spaces.
cout << "Commission for sales INR " << sales << " is = " << comm << endl;

getch();
}

Answer: The problem lies in the expression i.e. comm = (3/100) * sales. The calculation (3/100) is an
integer computation i.e. computer creates a temporary int variable to store value of (3/100 = 0.03
converted into int) which becomes zero.
To correct this, we must tell the compiler that the computation is floating type i.e. the expression
should be:
comm = (3.0/100.0) * sales;
or
comm = 0.03 * sales;

C. CBSE - Class XI - Computer Science - Simple C++ Snippets (Part-3)

IO manipulators

Q1: What is the purpose of iomanip.h header file?

Answer: The header file iomanip.h defines functions and operators to format input and output streams.
These are used in conjunction with insertion (<<) and extraction (>>) stream objects.

Although C++ defines a number of functions, Turbo C++ define a set of these. e.g.
numerical base formats: hex, oct, dec
setw: sets the width of the field
setprecision : sets the number of decimal places you want to display.
setiosflags: you may enable display stream flags (ios::showbase, ios::uppercase, ios::showpos)
resetiosflags: disable stream flags.

Q2: Write a program to display a number in hexadecimal, octal and decimal form.

Answer: The header iomanip.h defines hex, oct and dec format flags to display numbers in respective
number systems. (e.g. 15 will become 0f in hexadecimal notation and 17 in octal system).
By default C++ uses decimal format to display numbers. To display numbers in uppercase (e.g. 0F
and scientific numbers in uppercase
Similarly, to display base format e.g. (hexadecimal uses 0x notation), use manipulator
setiosflags(ios::showbase).
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>

int main()
{
clrscr(); // clear the screen
int x = 27;
cout << hex << "value of x in hexadecimal form:" << x << endl;
cout << oct << "value of x in octal form:" << x << endl;
cout << dec << "value of x in decimal:" << x << endl;
// show basefield notation, and in uppercase
cout << setiosflags(ios::showbase);
cout << setiosflags(ios::uppercase);
cout << hex << "value of x in hexadecimal:" << x << endl;
cout << oct << "value of x in octal form:" << x << endl;
cout << dec << "value of x in decimal:" << x << endl;

getch();
return 0;
}

The program will have the following output:
value of x in hexadecimal form:1b
value of x in octal form:33
value of x in decimal:27
value of x in octal form:033
value of x in decimal:27

Q3: An accountant is calculating profit and loss statement. He wants to show the numerical
values prefixed by '+' sign if they represent profit and with '-' sign as loss indicator. How can he
show the formatted output? Write a C++ program to show this.

Answer: The function setiosflags(ios::showpos) in iomanip.h turns on + sign for positive numbers.
Similarly, one can use resetiosflags(ios::showpos) to turn it off. Following snippet shows:
?
1
2
3
4
#include <iostream.h>
#include <conio.h>
#include <manip.h>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int main()
{
clrscr(); // clear the screen
int x = 27;
int p = 2, q = -4, r = 0;
float fval = 100.12;
cout << "p = " << p << " q = " << q << " r = " << r << endl;
cout << "fval = " << fval << endl;
cout << setiosflags(ios::showpos) << "\np = " << p << " q = " << q << " r =
" << r << endl;
cout << "fval = " << fval << endl;
cout << resetiosflags(ios::showpos) << "\np = " << p << " q = " << q << " r
= " << r << endl;
cout << "fval = " << fval << endl;

getch();
return 0;
}

Output on screen will be:
p = 2 q = -4 r = 0
fval = 100.12000

p = +2 q = -4 r = 0
fval = +100.12000

p = 2 q = -4 r = 0
fval = 100.12000
c:\tc\code>_

Q4: Write a C++ program to show how a number can be displayed as right or left justified. Also
show how can we set the width of the displaying field.

Answer: iomanip.h provides more iostream flags to align output to left or right. ios::left flag aligns output
to left, while ios::right aligns output to right. ios::internal justifies output internally.

The manipulator setw sets the field width of the output or input parameter.

By default, blankspace characters fills up the justification. You can change the fill-charatcer using
cout.fill(char) function.

Following snippet shows the use of these manipulators:
?
1
2
3
4
5
6
7
8
9
#include <iostream.h>
#include <conio.h>
#include <manip.h>

int main()
{
clrscr(); // clear the screen
cout << -123.45 << endl;
10
11
12
13
14
15
16
17
18
19
20
21
cout << setw(10) << -123.45 << endl;
cout << setw(10) << setiosflags(ios::left) << -123.45 << endl;
cout << setw(10) << setiosflags(ios::right) << -123.45 << endl;
cout << setw(10) << setiosflags(ios::internal) << -123.45 << endl;
cout.fill('*');
cout << setw(10) << 210.95 << endl;
cout << setw(10) << setiosflags(ios::left) << -210.45 << endl;
cout << setw(10) << setiosflags(ios::right) << -210.45 << endl;
cout << setw(10) << setiosflags(ios::internal) << -210.45 << endl;

getch();
return 0;
}

The output on screen will look like:
-123.45
-123.45
-123.45
-123.45
- 123.45
****210.95
-210.45***
***-210.45
-***210.45

Q5: A programmer is writing a module for a salary calculation package. After computing the
salary, he wants to display paisas upto two decimal points. Write a c++ program to show how a
floating number e.g. 12.578 is displayed as 12.57

Answer: C++ manipulators i.e. setf(ios::fixed), setf(ios::showpoint) and setprecision(int) be used
here.
The format option ios::showpoint displays decimal point and trailing zeros for all pointing point numbers,
even if decimal places are not need.
setprecision(2) ensures number of digits to be displayed after the decimal point.
The snippet is:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
#include <conio.h>
#include <manip.h>

int main()
{
clrscr(); // clear the screen
cout << 12.578 << endl;
cout.setf(ios::fixed); // display floating numbers NO scientific notation
cout.setf(ios::showpoint);
cout << setprecision(2) << 12.578 << endl; // displays 12.57
cout << setprecision(2) << 12.5 << endl; // displays 12.50
getch();
return 0;
}

D. CBSE Class 11 - Computer Science - Simple C++ Snippets (Part-4)

Sequence & Series

Q1: Write a program to to find the sum of series as:
S = 1
2
+ 2
2
+ 3
2
+ ... + n
2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// program to find sum of series
// S = 1^2 + 3^2 + 5^2 + ... + n^2
//
#include <iostream.h>
#include <conio.h>
#include <manip.h>

int main()
{
clrscr(); // clear the screen
unsigned int n;
unsigned long sum;
clrscr();
cout << "Enter range(n) = ";
cin >> n;
sum = 0;
cout << "S = ";
for (int i = 1; i <= n; i=i+2)
{
cout << i << "^" << 2;
if (i < n)
cout << " + ";
sum += i*i;
}
cout << "\nSum = " << sum << endl;
getch();
return 0;
}

The program will have the following output:

Q2: Write a program to sum the following sequence?
S = x + x
2
/2 + x
3
/3 + x
4
/4 +... + x
n
/n

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// program to sum the following sequence
// x + x^2/2 + x^3/3 + x^4/4 + ... + x^n/n
//
#include <iostream.h>
#include <conio.h>
#include <math.h>

int main()
{
clrscr(); // clear the screen
int x,n;
float sum = 0.0;
clrscr();
cout << "Enter the value of x= ";
cin >> x;
cout << "Enter the value of n= ";
cin >> n;

for (int i = 1; i <=n; ++i)
{
if (i == 1)
{
sum = x;
continue;
}
sum += ((float)pow(x,i)/(float)i);
}
cout << "Sum=" << sum;
getch();
return 0;
}

Q3: Write a program to find the sum of the following sequence:
S = x - x
2
/2! + x
3
/3! - x
4
/4! +x
5
/5! - x
6
/6!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// program to find sum the following sequence
// x - x^2/2! + x^3/3! - x^4/4! + x^5/5! - x^6/6!
//
#include <iostream.h>
#include <conio.h>
#include <math.h>

int main()
{
clrscr(); // clear the screen
int x, k;
double sum = 0.0;
cout << "Enter the value of x= ";
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
cin >> x;
k = -1;

for (int i = 1; i <=6; ++i)
{
k = k * -1;
if (i == 1)
{
sum = x;
continue;
}
// calculate factorial
double fact = 1.0;
for (int j = 1; j <= i; ++j)
{
fact *= j;
}
sum += ((double)pow(x,i)/fact)*k;
}
cout << "Sum=" << sum;
getch();
return 0;
}

The program will have the following output:

Q4: Write a program to print first 10 Fibonacci numbers.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// program to find sum the following sequence
// write a program to print first 10 Fibonacci numbers
//
#include <iostream.h>
#include <conio.h>

int main()
{
int n1, n2, n3;
n1 = 0;
n2 = 1;
// clear screen
clrscr();
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
for (int i = 0; i < 10; ++i)
{
// print first two fibonacci numbers
if (i == 0)
cout << n1;
else if (i == 1)
cout << n2;
else
{
// compute next fibonacci number
n3 = n1 + n2;
cout << n3;
n1 = n2;
n2 = n3;
}
cout << " ";
}
getch();
return 0;
}

The program will have the following output:

Q5: Write a c++ program to input integers in an array of size 10. Find the sum of even numbers
in that array.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// wap to input an integer array of size 10
// and find the sum of even numbers in an array
//
#include <iostream.h>
#include <conio.h>

int main()
{
int arr[10];
int i;
clrscr();
cout << "Enter 10 integers\n";
for (i = 0; i < 10; ++i)
{
cout << "Enter number [" << i+1 << "]=";
cin >> arr[i];
}
// compute sum of even numbers
long sum = 0;
int evens = 0;
22
23
24
25
26
27
28
29
30
31
32
33
for (i = 0; i < 10; ++i)
{
// check if number is even, then add
if (arr[i] % 2 == 0)
{
sum += arr[i];
evens++;
}
}
cout << "There are " << evens << " evens and their sum=" << sum << endl;
getch();
return 0;
}

Q6: Write a program to find the sum of digits.
e.g. if number entered is 2374, S = 2+3+7+4 = 16

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// program to find the sum of a number
// e.g. 123, sum = 1 + 2 + 3
//
#include <iostream.h>
#include <conio.h>

int main()
{
unsigned int num, digit;
clrscr();
cout << "Enter an integer number:";
cin >> num;
long sum = 0;
cout << "Sum of digits:" << num << "\n";
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num = num / 10;
cout << digit << "+";
}
cout << "=" << sum << endl;
getch();
return 0;
}

E. CBSE Class 11 - Computer Science - Simple C++ Snippets (Part-5)
User Defined Functions

Q1: Write a function to return the largest number among three integers

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// function to return the largest number among three integers
//
// function declaration
int largest_num(int , int, int);

#include <iostream.h>
#include <conio.h>

int main()
{
clrscr(); // clear the screen
int x, y, z;
cout << "enter 1st no.=";
cin >> x;
cout << "enter 2nd no.=";
cin >> y;
cout << "enter 3rd no.=";
cin >> z;
cout << "largest of three is=" << largest_num(x, y, z);
getch();
return 0;
}

// function largest_num
// input three integers
// output: returns largest number.
int largest_num(int a, int b, int c)
{
int max;
if (a>b)
{
max = a;
}
else
max = b;
if (max < c )
{
max = c;
}
return max;
}
Q2:What is function definition?
Answer: A function definition describes how the function computes the value it returns. It is a declaration
about the function. It is also called function prototype. In general it is defined before using the function,
within in the source file or in header (.h/.hpp) files.

Q3(Text Book): Write a C++ program that reads a float array having 15 elements. The program uses a
function reverse( ) to reverse this array. Make suitable assumptions wherever required.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// // program to reverse an array
//

#include <iostream.h>
#include <conio.h>

// functions declaration
void reverse(float arry[], int size);
void display(float arry[], int size);

int main()
{
clrscr(); // clear the screen
float flist[15] = {12.2, 8.1, 6.75, 18.25, 30.18, 12.5, 56.0,
78.12, 10.24, 13.12, 34.65, 16.80, 12.23,
32.5, 11.25};
display(flist, 15);
cout << "\nReversing the Array...\n";
reverse(flist, 15);
display(flist, 15);
getch();
return;
}

void display(float arry[], int size)
{
cout << "The list is:\n\t";
for (int i = 0; i < size; ++i)
cout << arry[i] << ", ";
cout << "\n";
}

void reverse(float arry[], int size)
{
float temp;
for (int i = 0; i <= size/2; ++i)
{
temp = arry[i];
arry[i] = arry[size - i -1];
arry[size - i - 1] = temp;
}
}
Q4(Text Book): Write a complete C++ program that invokes a function satis( ) to find whether four
integer a,b,c,d sent to satis( ) satisfy the equation a
3
+ b
3
+ c
3
= d
3
or not. The function satis( )
returns 0 if the above equation is satisfied with the given four numbers otherwise it returns -1.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// function satis()
/* wap to find whether 4 integers a,b,c,d sent to satis() satisfy the
eq. a^3 + b^3 + c^3 = d^3*/
//

#include <iostream.h>
#include <math.h>
#include <conio.h>

// functions declaration
int satis(int, int, int, int);

void main()
{
clrscr();
cout << "condition for F(2,3,4,6)=";
if (satis(2,3,4,5) == 0 )
cout << "true";
else
cout << "false";
cout << "\n\nCondition for F(6,8,10,12)=";
if (satis(6,8,10,12) == 0 )
cout << "true";
else
cout << "false";

cout << endl;
getch();
}

int satis(int a, int b, int c, int d)
{
if ((pow(a, 3) + pow(b,3) + pow(c,3)) == pow(d,3))
return 0;
return -1;
}

Q5(Text Book): Write a C++ program that uses following functions:
(i) sqlarge( ) that is passed two int arguments by reference and then sets the larger of the two
numbers to its square.
(ii) sum( ) that is passed an int argument by value and that returns the sum of the individual digits
of the passed number.
(iii) main( ) that exercises above two functions by getting two integers from the user and by
printing the sum of the individual digits of the square of the larger number.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// functions
/* wap that uses the function sqlarge() that is passed two int arguments by
reference & then sets the larger of the two nos.to its square*/
//

#include <iostream.h>
#include <conio.h>

// functions declaration
void sqlarge(int &, int &);
int sum(int num);

void main()
{
int first, second, sqnum;
clrscr();
cout << "Enter first number: ";
cin >> first;
cout << "Enter second number: ";
cin >> second;
sqlarge(first, second);
if (first > second)
sqnum = first;
else
sqnum = second;
cout << "Square of large number = " << sqnum;
cout << "\nSum of digits of square number = " << sum(sqnum);

getch();

}

void sqlarge(int &a, int &b)
{
if (a > b)
a = a * a;
else
b = b * b;
}

int sum(int num)
{
int s = 0;
while (num > 0)
{
s += (num % 10);
num = num / 10;
}
return s;
}
Q6: Write a c++ function swap( ) to swap to integers passed to the function.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// // function swap() to swap two numbers
//

#include <iostream.h>
#include <conio.h>

// functions declaration
void swap(int& x, int & y);

void main()
{
clrscr();
clrscr();
int a,b;
cout << "enter first no.=";
cin >> a;
cout <<"enter second no.=";
cin >> b;
cout << "1st No. (before swap)=" << a;
cout << "\n2nd No. (before swap)=" << b;
swap(a, b);
cout << "\n1st Number (after swap)="<< a;
cout << "\n2nd number (after swap)="<< b;
getch();
return 0;
}

// swap(), arguments passed as call by references.
void swap(int& x, int & y)
{
x = x + y;
y = x - y;
x = x - y;
}

F. CBSE Class 11 - C++ Snippets (Part-6)

Graphics Support in TC
Turbo C++ has support for various DOS mode graphics (CGA, VGA, SVGA etc.).

Various graphics functions are supported in GRAPHICS.LIB. You need to include <graphics.h> file in
Following are essential functions to be called to invoke graphics mode:

initgraph( ): initializes the application to work in graphics mode. It takes three arguments, DETECT,
graphics mode and path of *.BGI files.

closegraph( ): closes the graphics mode and switches the application back to test mode.

It supports various graphics functions (e.g. circle, setlinestyle, drawpoly, arc, ellipse...) to draw various
graphics primitives. You can fill the graphics object with different colour values and can control the color
palette.

Q1:Write a sample program to show graphics mode working of TC++

?
1
2
3
4
5
6
7
8
9
10
/* Simple example to draw circle */
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <stdlib.h>

void main()
{
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int gd=DETECT,gm;
initgraph(&gd, &gm, "c:/tc/bgi "); // initialize graphics mode.
/* read result of initialization */
int errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cout << "Graphics error:" << grapherrormsg(errorcode) << "\n";
cout << "Press any key to exit.";
getch();
exit(1); /* return with error code */
}
// randlomly draw 25 circles
int cx, cy, cr, cf, dr, clr;
for (int i = 0; i < 25; ++i)
{
cx = 80 + random(600); // center of circle (x-point)
cy = 80 + random(280); // center of circle (y-point)
cr = 20 + random(60); // inner radius
//cf = 1 + random(10); // color fill value
dr = 3 + random(20); // outer radius increment
clr = 1 + random(7); // random color
circle(cx, cy, cr+dr); // draw outer circle
circle(cx, cy, cr); // draw inner circle
//floodfill(cx, cy, clr);
setfillstyle(1 + random(10), clr);
floodfill((cx+cr+2), cy, WHITE);
}
//circle(330,180,100);
getch();
closegraph();
restorecrtmode(); // restore text mode
}

CBSE - Class XI - Computer Science - UNIT 1 - Computer
Fundaments - MCQs
Multiple Choice Questions on Computer Fundamentals

Q1: ASCII stands for

(a) American Standard Currency for International Interchange
(b) American Standard Code for Information Interchange
(c) American Standard Codecs for Information Interchange
(d) American Standard Code for International Interchange

Q2: Which of the following is related to Ist generation computers?
(a) Vacuum Tubes
(b) Transistors
(c) Large Scale Integration
(d) Multi-core processors

Q3:Full form of ALU is ___.

(a) Application Logic Unit
(b) Array Logic Unit
(c) Arithmetic Logic Unit
(d) Auxiliary Logic Unit

Q4: Antivirus is a type of which software?

(a) Application Software
(b) Utility Software
(c) System Software
(d) Firmware

Q5: Spreadsheet is an example of _____.

(a) Application Software
(b) Utility Software
(c) System Software
(d) Firmware

Q6: ____________ is data that has been organized or presented in a meaningful manner.

(a) Process
(b) Database
(c) Software
(d) Information

Q7: Compiler is an example _____.

(a) Application Software
(b) Utility Software
(c) System Software
(d) Firmware

Q8: Which of the following is not a DBMS?

(a) Oracle
(b) MS-Access
(c) Excel
(d) Foxpro

Q9: An error in a software is often referred as ___.

(a) squid
(b) leech
(c) bug
(d) virus

Q10: Personal computers use a number of chips mounted on a main circuit board. This main
board is often referred as:

(a) Motherboard
(c) Masterboard
(d) Fatherboard

Q11: Which one of the following is NOT a computer language?

(a) Oracle
(b) C++
(c) FORTRAN
(d) PASCAL

Q12: A computer program that converts an entire program into machine language is called
a/an

(a) Interpreter
(b) Compiler
(d) Simulator

Q13: Which of the following is a main memory?

(a) Hard Disk
(b) RAM
(c) Optical Disk
(d) Floppy Disk

Q14: Which of the following is not a multi-user operating system?

(a) DOS
(b) Windows 2003 Server
(c) Linux
(d) UNIX

(a) Computer Aided Design
(b) Complex Algorithm Design
(c) Computer Application Design
(d) Computer Accessory Design

Q16: 4 bits constitute to form ___
(a) A byte
(b) A nibble
(c) A kilobit
(d) Two Bytes

Q17: A computer is free from tiredness, monotony etc. reflects which characteristic?
(a) High Speed
(b) Accurate
(c) Versatile
(d) Diligence

Q18: Who is known as 'Father of Modern Computers'?
(a) Blaise Pascal
(b) Charles Babbage
(c) Von Leibniz
(d) John Neuman

Q19: UNIVAC was the first commercial computer designed in USA in 1951. What is the full
form of UNIVAC?
(a) Universal Array Computer
(b) Universal Automatic Computer
(c) United Array Computer
(d) United Automatic Computer

Q20: Who is the developer of C++ language?
(a) Van-Neumann
(b) Dennis M. Ritchie
(c) Donald Kunth
(d) Bjarne Stroustrup

Q21: Who was the developer of C language?
(a) Van-Neumann
(b) Dennis M. Ritchie
(c) Donald Kunth
(d) Bjarne Stroustrup

Q22: Who invented 'Analytical and Difference Engine'?
(a) Van-Neumann
(b) Charles Babbage
(c) Donald Kunth
(d) Von Leibniz

Q23: Who is known as 'Father of Analysis of Algorithms'?
(a) Van-Neumann
(b) Dennis M. Ritchie
(c) Donald Kunth
(d) Bjarne Stroustrup

Q24: MS Excel is a/an ____.
(a) word processor software
(b) image processing software
(d) multimedia player

Q25: Which one of the following is an impact printer?
(a) Inkjet printer
(b) Daisy wheel printer
(c) Laser printer
(d) Thermal image printer

(more to come...)

1. (b) American Standard Code for Information Interchange
2. (a) Vacuum Tubes
3. (c) Arithmetic Logic Unit
4. (b) Utility Software
5. (a) Application Software
6. (d) Information
7. (c) System Software
8. (c) Excel
9. (c) bug
10. (a) Motherboard
11. (a) Oracle
12. (b) Compiler
13. (b) RAM
14. (a) DOS
15. (a) Computer Aided Design
16 (b) a nibble
17 (d) Diligence
18 (b) Charles Babbage
19 (b) Universal Automatic Computer
20 (d) Bjarne Stroustrup
21 (b) Dennis M. Ritchie
22 (b) Charles Babbage
23 (c) Donald Kunth
25 (b) Daisy wheel printer

CBSE Class 11/10 - Computers/FIT - Programming
Languages
Programming Languages

Q1: What is a computer language?

Answer: The instructions are fed into computers in the form of computer programs, follow some
semantics (grammar or syntax) collectively is called a programming language. In layman terms, the
language, which the user employs to interact with computer, is known as computer or programming
language.

Examples are: C/C++, Java, Pascal, COBOL, Python, FORTRAN etc.

Q2: What are different generation of languages?

Answer: A good number of languages have been developed so far, therefore all computer languages are
divided among different generations of languages:
1. The first generation languages (1GL) are low-level languages that are machine language.
2. The second-generation languages (2GL), ar also low-level languages that consist of assembly
language.
3. The third generation languages(3GL) are high-level languages such as C.
4. The fourth generation languages (4GL) are languages that consist of statements similar to
statements used in a human language.
5. The fifth generation languages(5GL) are programming language based around solving problems
following constraints-driven approach rather using an algorithm-driven approach.A good example
of a fifth generation language is PROLOG.
Q4: What were different types of problem-solving approaches which led to the basis of computer-
based solving and design of computer languages?

Answer: Various problem solving approaches which were initially used in computer based solving and
design of computer languages are:
1. Look for similar patterns or situations (Table look up)
2. Divide and Conquer
3. Heuristics (rule of thumb)
4. Algorithms

Machine Code
(Hex View of .exe file)
Q5: What is a machine language?

Answer: Machine Language was the first generation programming language which is hard wired in
processors. It is also referred as machine code or object code. Machine language instructions are built
into the hardware of a particular computer. It is a collection of binary digits or bits that the computer reads
and interprets. Machine language is the finite number of instructions that only a computer can
understand.

Q6: What are the advantages of First Generation Languages?

Programs written in machine language execute very fast by the computer.
The computer directly understands machine instructions, since no translation of the program is
required.

Q7: What are the disadvantages of First Generation Languages?

1. Machine dependent: The processor and machine code are directly integrated the machine
language differs from computer (processor) to computer (processor).
2. Difficult to Code: Although, machine language programs are directly executed by the computer,
but it is difficult to write program in machine language.
3. Error Prone Coding: a programmer has to remember the machine codes, and must keep track of
the storage locations of data and instruction. So it becomes very difficult to concentrate on the
logic of the problem. This frequently results in programming errors.
4. Difficult to understand: It is difficult to correct or modify machine language programs.
5. High Cost of Maintenance: Because of tight integration with processor high chances of error
coding, cost of coding and managing programs increase.

Q8: What is an assembly language?

Answer: Assembly language is a second generation programming language. Also referred as low-level
programming language, it is designed for specific processors. Assembly language uses structured
commands as substitutions called mnemonics. The mnemonics are used for bits used by machine
language allowing humans to read the code easier than looking at binary.

A system software called assemblers were designed to convert mnemonic codes into machine language
instructions. In general assembly mnemonic instruction consist of Opcode (CPU instruction) and operand
(data value).

Here is an example of assembly language code.
mov ax,2C00h - Move number (2C00H) to AX register
inc cx - Increment value stored in CX register
int 20h - Call Interrupt 20H

Since mnemonics are one to one related to machine code instructions, assembly language is machine
dependent.

Q9: What is a computer bug?

debugging
credits:openclipart

Answer: A computer bug is a any type of problem, defect or mistake in a program/software or in a
hardware.

Q10: What is debugging?

Answer: The process of identifying and removing a defect in hardware or software is called debugging.

Q11: What are the advantages of assembly language over machine language?

Answer: As compared to machine language, assembly language has the following advantages:
1. Easier to write code.
2. Easier to debug program.
3. Easy to modify program, reduce maintenance cost.
4. Useful for writing small, real time applications for embedded devices.

Q12: What is an assembler?

Answer: Assembler is a system program which translates an assembly language code or program to
machine code format. Generally assemblers are written by the manufacturers of processors because
assemblers are machine dependent.

Q13: What are the limitations of coding in assembly language?

Answer: Following are the limitations of coding in assembly language:
1. Machine Dependent: Since mnemonics are processor specific, one needs to port or translate
his/her program while switching to another machine (processor model).
2. Hardware knowledge is prerequisite. It because necessary to know about CPU and its hardware.
3. Increase maintenance cost. Due to above said two factors, it increases the maintenance cost for
managing software.
Q14: What are high-level languages?

Answer: High level languages or 3rd generation languages use English like statements to code computer
instructions. These are programmers' friendly since these languages are machine-independent and
developers can concentrate on problem solving logic rather than on hardware structure. These languages
require system programs called compilers (and interpreters) which translate English-like statements
(source code) to machine code.

Examples of high level languages are: C, C++, Java, COBOL, BASIC, FORTRAN, Pascal etc.

Q15: Write full form of following computer language abbreviations
(i) COBOL
(ii) BASIC
(iii) FORTRAN
(iv) ALGOL
(v) LISP

(i) COBOL: Common Business Oriented Language (used for developing for business applications)
(ii) BASIC: Beginners All purpose Symbolic Instruction Code
(iii) FORTRAN: Formula Translation (for developing scientific applications)
(iv) ALGOL: Algorithm Language
(v) LISP: List Processor (used for developing artificial language based applications)

Q16: What is a compiler?

Answer: Compilers are system programs, which translate a source code written in high level language to
an object code. When a programmer writes a program code, he runs the compiler program, which
translates it into an executable program. Then you run or execute the translated program.

In general compiler works in multiple stages. In the first stage it checks if the code follows the high level
language grammar and semantics. If there is any error(s) it aborts the compilation process and reports
the errors. If there is no error, it generates intermediate object code. In the second stage, linker which is a
system program part of compiler, combines various object code files into a single executable binary file
(machine code).

Q17: What is an interpreter? What are advantages and/or disadvantages the interpreter has over
compiler?

Answer: An interpreter like a compiler is also a system program that translates a high-level language
code into a low-level one. But the translation occurs at the moment when the program is run. A developer
writes the code using a text editor or something similar, and then calls the interpreter to translate and run
the program. Interpreter reads the source code one line at a time and translates each line and then
executes it. Then it translates the second line, runs it, and so on.

The main advantage of interpreters over compilers is that a syntax error in the source code is detected
and is brought to the attention of the programmer as soon as the program statement is interpreted.

The main disadvantage of interpreter over compiler is that it is slower than compiler when running a
finished program. Also the interpreter's run time environment has to be available in the computers
memory when the program is executed.

GW-BASIC is an interpreter for BASIC language. Visual C++, Turbo Pascal are compilers for C++ and
Pascal languages. Now-a-days, modern languages like Java, C# use a mixed model of interpreter and
compiler.

Q18: What are the main advantages of High Level Languages?

Answer: High Level language have the following advantages over using assembly and machine
language:
1. Machine Independent: A high level language written code can easily be ported to another
hardware platform. Language instructions do not reply on characteristics of a machine hardware.
2. Easy to Learn: Since the HLL instructions are English like statements, these are easier to learn.
3. Closer to Design Process: The design process is better suited for HLLs as compared to low level
languages. Implementing various programming techniques, algorithms and data structures is
much easier in HLLs.
4. Few Errors and Easy Debugging: Since the programmer need not worry about hardware
instructions, he can concentrate more on developing program logic.
5. Low development Cost: Writing programs in high level languages take less time as compared to
coding in low level languages.
6. Low maintenance cost: Programs are easier to manage. One can find errors easily and rectify
them thus reducing the maintenance cost.
7. Better Documentation: Since HLLs are English language statements and hence easier to
understand. The code is self-explanatory and requires less commenting.
Q19: What are the limitations of High level languages?

Answer: Although HLLs have numerous advantages over low level languages but it does have certain
limitations:
1. Large time to execute: A HLL translated machine code is bigger in size than low level
programmed code. Thus HLL translated code takes larger time to execute.
2. Less optimized code: Sometimes it is required to write a small sized efficient programs because
of memory and performance constraints (e.g. embedded devices which use CPUs do not have
large memory and need faster response). In this case, coding in assembly is better alternative.
3. Managing Growing complexity is a challenge: As the code size of a software grows, the
complexity to manage its code also increases. One needs to apply various disciplinary actions of
software engineering and code management to produce a reliable software. This indeed
increases the managing cost.

Q20: Who designed 'C' language?

Q21: Who designed 'C++' language?

Q22: Who designed BASIC language?

Answer: John George Kemeny and Thomas Eugene Kurtz at Dartmouth College

Q23: Who developed Visual Basic?

Q24: Who is known as Father of Computers?

Q25: Name the language which was named after a lady who was the daughter of poet Lord
Byron. She worked with Charles Babbage to develop plans for the Analytical Engine.