More questions on the basics (chapters 1 to 3)
Q.) Can we write a program as the one below:
int main( )
{
int x;cout<<"\nEnter the value of x:";cin>>x;cout<<"You entered:"<<x;
return 0;
}
A.) Yes. The program is valid since the compiler breaks up C++ statements on the basis of the semicolon and not on the basis of the new-line. The above program is equivalent to writing:
int main( )
{
int x;
cout<<"\nEnter the value of x:";
cin>>x;
cout<<"You entered:"<<x;
return 0;
}
Q.) What are tokens? What’s lexical analysis?
What is the output of:
int i=5,j=2;
cout<<endl<<(i---j);
i=5,j=2;
cout<<endl<<(i-- -j);
i=5,j=2;
cout<<endl<<(i- --j);
A.) Every C++ statement we write can be broken down into tokens (also called symbols). The compiler goes through a number of steps before finally converting high-level language into object code and one of these steps is ‘lexical analysis’ (this stage follows the preprocessing stage). In this stage (lexical analysis) the compiler identifies all the tokens used in the program. Operators, identifiers, keywords etc. are all tokens (white space is not a token but white space helps separate tokens). Every C++ statement is a combination of tokens. For example, in the statement:
a=b+c;
the symbols are: a,=,b,+,c
a, b, c are identifiers and +, = are the operators.
A question arises, how does the compiler decide how long a token is? How does the compiler know when to consider one character for a token and when to consider more? To simplify this situation, the compiler tries to extract the largest possible chunk in a statement to form a token (in other words it’ll try to extract the largest token while scanning from left to right). In our statement:
a=b+c;
the compiler cannot consider a= as a single token (because it knows that = is the assignment operator). So it picks ‘a’ as a symbol and so on. This might not seem to be a significant point but it’ll help us tackle the second question:
int i=5,j=2;
cout<<endl<<(i---j);
We’ve used three consecutive –'s in i---j. You might be tempted into thinking this would cause a compile error but it won’t because of the way lexical analysis is performed. Remember: always extract largest possible token. In i---j, the compiler will find i-- as the largest token. i--- can’t be considered because we don’t have any such operator and i alone would be the smallest possible token. Another possibility is to chose i- but the compiler is designed to take the largest token and that is: i--.
Next it is left with -j which is equivalent to the unary - operator.
Thus the expression reduces to:
i-- -j;
which is as good as 5-2 (because in i-- we’ve used the postfix decrement operator).
In the next two examples:
i-- -j
and
i- --j
since there is a blank space (white space), the compiler doesn’t have an option of deciding upon the symbol. The resultant output for the code fragment will be:
3
3
4
Note: In the previous question the following was termed illegal:
k = i+++++j;
In this case, the compiler would break up the statement as:
k = (i++)++ + j;
Upto k = i++ everything is fine, but after that it can’t form a legal token and thus produces a compile time error. It will form:
(i++)++
which is not valid.
It is better to avoid coding statements like i---j even though they produce the desired result (such code is neither pleasing to the eye nor the mind!).
Q.) Deduce the output in both of the following cases:
a.)
int x=5;
if ( (x= =5) || (x=7) )
{
cout<<"True";
}
cout<<x;
What do you think is the value of ‘x’?
A.) Since ‘x’ is 5, the first condition is satisfied. When we use an OR operator, the program will not evaluate the second condition if the first is true. Thus the value of ‘x’ will be 5 and the output will be: True5
b.)
int x=5;
if ( (x= =5) && (x=7) )
{
cout<<"True";
}
cout<<x;
What do you think is the value of ‘x’?
A.) Since ‘x’ is 5, the first condition is satisfied. When we use an AND operator, the program will evaluate the second condition if the first is true. In the second condition we have assigned a value of 7 to x. Thus the value of ‘x’ will become 7 and the program will output: True7
Q.) Will the following program (not having an iostream.h) compile and execute? Explain with reasons.
int main( )
{
return 0;
}
A.) iostream.h is needed when we use cin, cout, <<, >> and similar objects (which we use in most of our programs). Otherwise you can do without iostream.h and the above program will compile and execute successfully.
Interview/Viva/ Test questions (along with the logical questions solved earlier):
Other Questions and Programs:
Q.) What is an interpreter?
Interpreters are programs which will interpret the source code line-by-line and execute them. Compilers, on the other hand, read through the entire source code and convert it into an object code (they do not execute the code). The UNIX/Linux shell is a command interpreter (it executes your instructions on a line-by-line basis). The DOS command.com is an interpreter. Interpreters do not produce object code (which means that each time you run a program in an interpreter, the interpreter will repeatedly check the syntax etc. before executing the code). There are some languages which are called interpreted languages (i.e. these languages are always interpreted). C/C++ is not an interpreted language.
Q.) What is a linker and a loader?
Generally when writing a program, you will make use of different libraries and other source files. On compiling each source file we obtain an object code. All the object codes need to be put together to form an executable (because one code might call a function written in another code and only the linker will be able to resolve this). This is the basic function of a linker.
A loader is generally a program which loads the program (the executable) into main memory. All our programs and applications reside on a secondary storage device (hard disk) but the processor requires programs to be present in the main memory (RAM)- access time for main memory is much lower than secondary storage (i.e the CPU can access main memory quicker than secondary memory). The loader takes the program from secondary and loads it into main memory.
You will be clear about a linker when we discuss about multiple file programming.
Q.) Distinguish between machine language, assembly language and middle level languages?
Q.) Why do we need compilers?
Q.) What is the difference between a constant and a variable?
Q.) Write a C++ program to input the name and age of the user and then display a sentence on the screen along with these two data.
Q.) Write a program to convert a given amount from US dollars into Indian Rupees (assume the conversion rate as USD 1 = Rs.45). Also display a statement.
Q.) Write a program that will calculate the compound and simple interest on a principal amount for a given number of years (also obtain the rate of interest from the user).
Q.) Write a program to help a shopkeeper calculate the percentage profit he makes when he sells an item (obtain the buying price and selling price as inputs from the shopkeeper).
Q.) Write a program to solve quadratic equations of the form:
ax2 + bx + c = 0
Obtain the values of a, b and c from the user and find out the values for x.
Copyright © 2004 Sethu Subramanian All rights reserved.