Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Directions: For Information On EOF Refer To Question Bank #1, Part-A, Q4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

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 13 objective type questions on C.
5. Hints and useful concepts are summarized in Appendix A & Appendix B for selective
problems from Part-A only.

PART - A

if-else construct is extremely important in any language. However, one of the problems with if-
else statements is that they can grow very complex when nested. One alternative is to use
switch construct. However, switch statements cannot always be substituted for all nested if-else
logic. Observe the code given below and predict the output, carefully noting the pattern.

Mastering switch construct is important. But switch can be tricky when there are multiple cases
and subsequent break statements involved. To better understand , WAP (write a
program) to allow a user to enter input until she enters EOF character (for information on EOF refer
to Question Bank #1, Part-A, Q4) and display the distribution of vowels (irrespective of case)

Carefully analyze the code written below and understand how it works. Test the code by
running it and giving sample test cases. If you face difficulty trying to understand the logic, run
it on a sample test case line by line. This activity is called dry-running, where by you trace the
execution pattern of a program on a piece of paper. This is one of the best ways to get
acquainted with the algorithm that you didn’t write.
}

The most important loops in C are and for. Theoretically every for loop can be converted
into an equivalent while loop (see review notes on this problem). Mastering while and for loops
are extremely important. Observe the code given below which simply reverses a string
(character array passed). After dry-running the code (see previous question on hints on dry-
running) convert the following for loop into a while loop, without adding or subtracting any
functionality.

/* reverses the string passed in variable s */

// ... main method () and rest of the code ...

Two most handy coding constructs used in conjunction with loops are and
statements. A break statement, when encountered immediately causes the innermost block (a
loop or a switch statement) to be exited. In this exercise we precisely need this functionality.
Allow a user to specify an arbitrary input. Write a function to remove all trailing (present in the
end) whitespace characters (i.e. space – ‘ ‘, newline ‘\n’ and tab ‘\t’).
Write a program to sort 10 numbers in an array. If you’re not familiar with sorting algorithms,
first read elementary text on some of the most common sorting procedures. You must ensure
that you have clear knowledge basic sorting algorithms (bubble sort, insertion sort, selection
sort). These are very commonly asked in interviews and written tests.

Read about scanf() and printf(). Observe the exact nature and type of arguments that these
routines take. Also learn about the values which these functions return. Experiment with field
specifiers and type specifiers and other formatting fields which scanf() and printf() use. WAP
which makes appropriate use of these formatting specifiers.

PART – B

( )

What will happen if you write, compile and then run the above program ?

a.) It will not compile.


b.) It will print out: i=9.
c.) It will print out: i=11.
d.) It will loop indefinitely.

Which one of the following printf () format specifiers indicates to print a double value in
decimal notation, left aligned in a 30-character field, to four (4) digits of precision?

a.) %-30.4e
b.) %4.30e
c.) %-4.30f
d.) -30.4f

What will be printed when the sample code above is executed?

a.) 0
b.) 1
c.) 4
d.) 5

Predict the output of the following piece of code:

Printf(‚%d‛, iZ);

a.) 5 b.) 6 c.) 10 d.) 11

"I’ll score at least 95%!"

Select the statement which will EXACTLY reproduce the line of text above.

a.) printf("\"I’ll score at least 95/%\!\"\n");


b.) printf("I’ll score at least 95%!\n");
c.) printf("I’ll score at least 95%'!\n");
d.) printf("\"I’ll score at least 95%%!\"\n");
In C, declaration is different from definition along which of the following factors?

a.) Both can occur multiple times, but a declaration must occur first.
b.) There is no difference between them.
c.) A definition occurs once, but a declaration may occur many times.
d.) A declaration occurs once, but a definition may occur many times.

What value will contain in the sample code above?

a.) 22 b.) 26 c.) 46 d.) 50

If you have to qualify C to be a specific type of language, which of the following will you take?

a.) Machine b.) Procedural c.) Assembly d.) Object-oriented

What is the value of myArray [1][2] in the sample code above?

a.) 1 b.) 2 c.) 3 d.)5

What will be printed when the sample code above is executed?


a.) x=0 b.) x=1 c.) x=3 d.)x=4

What value will x contain when the sample code above is executed?

a.) x=1 b.) x=2 c.) x=3 d.)x=4

What will the code above print when executed? Hint: ceil rounds up 3.2 to 4 and floor rounds down 3.2 to 3

a.) -3 : 4 => -4 : 3
b.) -4 : 4 => -3 : 3
c.) -4 : 3 => -4 : 3
d.) -4 : 3 => -3 : 4

After the sample code above has been executed, what value will the variable x contain?

a.) 4 b.) 5 c.) 6 d.) 7


Appendix A. – Important Hints and Resources (Question Wise)
Q 1.) Remember that an if statement is allowed to carry no accompanying else, however, there
can’t be an else construct without a corresponding if.

Q 4.) Both for and while keeps a block of statement running until an expression evaluates to
true i.e. non-zero. In order to convert a for loop to a while loop, simply take out the
initializing part and increment part and place them before and within while loop
respectively.

Q 5.) For this problem consider starting your iteration from back of the array passed into the
function. i.e. try and follow the following coding convention.

Moreover, as you decrement the counter (len variable) check to see if each character is
indeed a whitespace character, if it’s not readjust the ‘/0’ character immediately to mark
the end of the character array.
Appendix B. – Important Concepts (Question Wise)
Q 2.)

- One of the main problems with switch statement is that, we cannot put logical
expressions as switch labels. Switch can only do a ‘==’ comparison between its argument
and labels involved. If the logic requires you to do several logical comparisions on a
single argument, it can’t be achieved by switch. For e.g. it is a wrong use of switch.

// wrong usage, can’t compare x using logical operators.

Q 4.)

- A while is governed by following expression.

This will make the body of while loop run as long as expression evaluates to true (i.e. a
non-zero value)

- A for loop is governed by three expressions and the format of a for loop is

However, every for loop can be converted to an equivalent while loop according to
following rule.

- Any of the three expression in for loop can be skipped, However, semicolons must be
written; Following is a perfectly legal statement in C but it will run for infinite time
unless otherwise break down by an external break of return in the body of the loop
Q 5.)

- Whitespace characters are special characters which makes special sense to compiler. The
default behavior of encountering a whitespace character is that any input seeking
function (like getchar(), scanf(), fscanf() ) stops to consider further input after it
encounters a whitespace character. You are free to define your own whitespace
characters by modify the environment variable, but by default they are space, newline
(carriage return on windows) and tab. i.e. if you supply a space (or a tab or newline)
then scanf() will only read till that point, in a single invocation.

- continue is another construct frequently used with loops. The continue statement causes
the next iteration of the innermost loop to execute immediately.

- In C a string is represented as a loose array of characters (char types). We need a


mechanism to allow the functions to read a character array only up to its designated
length. Since, there are no classes in C, we can’t externally store and update the length of
an arbitrary array. As a consequence, whenever we have to pass an array (of any type)
into a function, we must also pass the length, or else that function might try to access the
array past its end, which might cause program crash. To circumvent this problem, as a
convention all strings (char arrays) are marked with ‘/0’ to mark their end. The good
thing about this strategy is that we can (at our will) rearrange this ‘/0’ to any byte
(character) in the array to decrease / increase the length. The bad part is that if we
construct the string ourselves (which copying or concatenating) we have to keep an eye
out for placing ‘/0’ ourselves.

You might also like