Vous êtes sur la page 1sur 8

Directions

1. All the questions/programs are required to be typed, complied, run and then observed.
2. If possible try experimenting with concepts revealed by each question.
3. Part-A, covers 7 questions on Standard C.
4. Part-B, covers 3 questions on Standard C++.
5. Part-C, covers 8 objective type questions on both C and C++.
6. Hints and useful concepts are summarized in Appendix A & Appendix B for selective
problems from Part-A only.

PART - A

Q 1.)

Which of following are legal Identifiers in Standard C? Remember that no illegal identifiers can
be used to name variables or functions in a program. Hence, you can assign following
identifiers to variables and see if they are acceptable, paying key attention to error reported by
compiler?

1) __45
2) _LPU
3) LPU__
4) 1_LPU
5) __LPU__

Q 2.)

There are in total natural data types defined by Standard C.

1) 2) 3) 4)

All these basic data types can also be modified by following two modifiers:

1) 2)

Further and data types can also be or . Write a program to display the
range of values which can be represented by all these data types. You don't have to necessarily
use operator. Instead take a peek in a header file called < > or < > and
simply display all the constants written in them. These constants directly tell you the range of
values applicable to each data type on a specific machine. Try and understand how signed
variables are represented in machine.
Q 3.)

Understanding is very important in C. There is usually more than one mechanism to


specify constants in C. For instance all of the following are ways to specify an integer value
(char, int, short, long) of 127.

1.) var = 127; 2.) var = 0xff; 3.) var =0377;

After studying the ASCII character set, write a program to print the characters having the ASCII
code from 0 – 127. Observe that some characters can’t be printed; take keen note of such
characters. For bonus points, try displaying them in a table format with first column specifying
ASCII value and second column displaying the corresponding character.

Q 4.)

Read about getchar(). It’s one of the most easy to use input mechanisms. which read only one
byte from standard input. After gaining a firm understanding of ASCII codes from previous
problem, write a program to keep taking input from user until she enters EOF character. EOF
character is usually marked by ctrl-d on unix/linux and by ctrl-z on windows. But to detect it,
you’ll have to know the ASCII of EOF character. (To avoid knowing ascii of EOF character all
together see hint on this problem)

Q 5.)

Write a function htoi (s), which converts a string of hexadecimal digits into an equivalent
integer value. The allowable digits are 0-9 and a-f, and A-F. You must understand how to inter-
convert between various number systems. Read carefully about conversion rules and
techniques. This is extremely important for your written tests and interviews, hence heavily
experiment by considering various online resources.

Q 6.)

Read in depth about pre/post increment operators and try to observe the output obtained after
executing following statements for two variables and .

1.) v1++ = ++v1+(++v2);


2.) v2++ += ++v2;
3.) v1 = (v1++) + (++v2);

To reinforce the understanding of pre/post increment operators, heavily experiment with all
possible combinations. Consult a teacher if you get confused, but make sure you master the
questions on pre-increment and post-increment, these are very common in written tests and
interviews.

Q 7.)

Write a small program to swap the values of two variables through an external function
having following signature . If this doesn’t work (i.e. values doesn’t
swap), find out all possible ways for this to work.

PART – B

Q 8.)

Search for an article illustrating the principles of encapsulation and function overloading in
C++. After careful analysis and understanding write a program to illustrate the concept of
function overloading and encapsulation.

Q 9.)

Imagine a customer walking up to you and asking you to make a program for a game of chess.
Identify all the key objects, their behavior and data relevant towards modeling this problem.

Q 10.)

Referring to Q 7 in Part-A, there’s an additional mechanism in C++ to swap two variables via. an
external function, through references. Read in detail about how references work in C++. Write a
program to swap values of two variables via references.

PART – C

Which of the following techniques can you use to obtain the digit value from a char variable (var) having
a digit.

1.) getDigit (var) 2.) (int) var 3.) var-“0” 4.) var-‘0’

Which of the following lines might generate an error/warning, when is an int variable and is a
float variable.

1.) v2 = 6.7f 2.) v1 = v2 3.) v1 = (int) v2 4.) v1 = v1 + v2


What is the result of the following operations:

1.) Program Crash 2.) Will not compile 3.) -11 4.) -1.

How many times will the following while loop run?

1.) One million 2.) Infinite 3.)Only once 4.) None

What will be the output of following lines of code?

1.) Program Crash 2.) Will not compile 3.) Infinite Looping 4.) Syntax Error

What will be the output of following line of code?

1.) Program Crash 2.) Will not compile 3.) Infinite Looping 4.) Syntax Error

What will be output of following program?

1.) Program Crash 2.) Will not compile 3.) I can do it 4.) I cannot do it
What is the output of the following line of code? (assume, a is an ‘n’ element array) ?

1.) Prints numbers from 0 – n, separated by a – ‘ ‘.


2.) Prints numbers from 0 – n, separated by a – ‘\n’.
3.) Prints numbers from 0 – n, separated by a – ‘ ‘, such that there’s a newline after every
element.
4.) Prints numbers from 0 – n, separated by a – ‘ ‘, such that there’s a newline after every
element.

Appendix A. – Important Hints and Resources (Question Wise)


Q 1.) Observe that the purpose of any language is to deal with real world entities. In guessing
if an identifier can be used legally, just ask yourself, if that identifier can be used to
name a real life entity of your choice i.e. name of a person or a place. This trick will work
most of the times, but not always. For exact rules and exceptions experiment with a
sample program.

Q 3.) Since, char types are basically integer types, you can define a simple for loop and iterate
over the iterator variable for bounds from 0 to 127, printing it for each iteration.
However, some characters will not be printed (like /12 – newline) since these are non-
printable characters.

Q 4.) To avoid having to know the ascii value of EOF character on a particular platform, you
can simply compare the value returned by getchar() to EOF constant defined in stdio.h.
This constant is automatically mapped to the correct key combination of specifying the
EOF on a particular platform and hence, you don’t have to bother about its
implementation.

Q 5.) To attack this problem, you must know the ascii bounds of characters from a-f, and A-F.
moreover, you need a mechanism to find if a character is indeed a digit from 0-9. To ease
your burden, try looking in the header cctype.h, it has a couple of really handy character
based functions like (tolower(), isdigit()), you can directly use them.
Appendix B. – Important Concepts (Question Wise)
Q 2.)

- It is very important for you to understand that negative numbers are represented either
directly in signed representation or two's complement notation.

- Sometimes we need unsigned numbers in our programs to represent concepts which


can't possibly take negative values (e.g. Roll No, Age), in such situations it's a good idea to
qualify our variable with unsigned keyword. However, if later we'll try to assign this variable
(accidently) a signed variable - either directly or through casting. We'll face problems. Since, the
compiler will try to ignore the signed bit, which will alter the value of variable being assigned.

- Moreover, one needs to understand that range and size of a data type is never
guaranteed by the language. Instead standard C only guarantees a lower limit on the size of the
variable. Doing so forces the programmers to write code which is within the bounds of these
lower limits, so that they can be ported to other machines safely.

- For unsigned numbers, if you go over the range, the result is simply modulo . i.e. for a
n bit integer number i.e. if you assign a value of 271 to a char variable ( ) the result
stored will be 271%256 = 15

Q 3.)

- It is always considered a bad habit to let constants being provided in their original form.
To alleviate this problem, constants which define a concrete concept in your program
(maximum array size, age limit, thresholds) are provided in one of the following standard
notations.

1.) Enums

2.) As const variables in CAPITAL LETTERS.

3.) As #define pre-processor elements.


Q 4.)

- EOF character is an important character for handling input/output in C. EOF – End of


File, basically specifies the un-willingness of user to enter any more data. Hence, most programs
which deal with arbitrary length input, see for this character to end inputting.

- EOF character is entered by ctrl+z on windows and ctrl-d on linux. However, these
conventions change with versions of shell.

- Some programmers use some predefined sentinel values (like a particular key
combination) instead of an EOF character. This is to make program more user-intuitive,
since not everybody can remember the exact EOF mapping. But one can easily
remember a sentinel value)

Q 5.)

- hexadecimal numbers have in total 16 digits, 0-9 and A-F, where A = 10, B = 11 … F = 15.
Any number (positive/negative) can be encoded in its hexadecimal format. To convert a
number into hexadecimal, try and mimic the same pattern which you do in representing
a number in decimal system. Take this example for clear understanding.

Suppose I ask you to find out how much does 12305 represents in decimal format. Now,
first of all understand that I’ve asked you find the value in decimal, which as base 10. So
the value of the following figure is (starting from right to left)

5x +0x +3x +2x +1x = 5 + 0 + 300 + 2000 + 10000 = 12305

Now, if I would have asked you to represent the same number in hexadecimal:

5x +0x +3x +2x +1x = 5 + 0 + 768 + 8192 + 65536 = 74501

You can do the same thing for octal, where base is 8.

5x +0x +3x +2x +1x = 5 + 0 + 192 + 1024 + 4096 = 5317

- A quick thing to observe is that as you reduce the base, the same sequence of digits will give a
lower value; as a consequence we need more number of digits to represent the same number in
a lower base system. This is particularly true for binary numbers.
Q 7.)

- You cannot possibly swap the values of two variables through call by copy (also called
call by value) mechanism. You’ll necessarily have to use pointers. You can pass the
address and then dereference it to swap the values.

Vous aimerez peut-être aussi