Friday, 7 June 2013

Loop Question 2:

What will be output of following c code?
        
#include<stdio.h>
int main(){
    int i=2,j=2;
    while(i+1?--i:j++)
         printf("%d",i);
    return 0;
}



EXPLANATION


Output: 1
Explanation:
Consider the while loop condition: i + 1 ? -- i : ++j
In first iteration:
i + 1 = 3 (True)
So ternary operator will return -–i i.e. 1
In c 1 means true so while condition is true. Hence printf statement will print 1
In second iteration:
i+ 1 = 2 (True)
So ternary operator will return -–i i.e. 0
In c zero means false so while condition is false. Hence program control will come out of the while loop.

Loop Question 1

What will be output of following c code?

#include<stdio.h>
extern int x;
int main(){
    do{
        do{
             printf("%o",x);
         }
         while(!-2);
    }
    while(0);
    return 0;
}
int x=8;



EXPLANATION


Output: 10
Explanation:
Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8
There are two do-while loops in the above code.  AS we know do-while executes at least one time even that condition is false.  So program control will reach  at printf statement at it will print octal number 10 which is equal to decimal number 8.
Note: %o is used to print the number in octal format.
In inner do- while loop while condition is ! -2 = 0
In C zero means false.  Hence program control will come out of the inner do-while loop.   In outer do-while loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.

Tuesday, 21 May 2013

QUIZ 26-30 ANSWERS

26. 
_____ variables remain in memory until the program ends
Area
Global
Local
Reference
Value
Answer: Option B

27.
The outline or the definition of a function is called its
beta test
Forerunner
Outline
Prototype
Answer: Option D

28. 
Which of the following is false?
Data stored in an array can be accessed faster than data stored in a disk file
Data stored in an array needs to be entered only once, typically at the beginning of the program
Arrays allow the programmer to store information in the computer's internal memory
When using arrays, you will have fewer variable names to remember
None of the preceding statements are false
Answer: Option E

29. 
A function typically contains
the function header
the function braces
the return statement, if it's value- returning function
a display message
All of the above
Answer: Option E


30. 
A variable's _____ indicates which portions of the program can use the variable
Area
extent
Lifetime
reach
scope
Answer: Option E

Monday, 20 May 2013

QUIZ 21-25

21. 
A group of related fields that contain all of the data about a specific person, place, or thing is called a
data file
field file
program file
Record
Answer: Option D

22. 
To send output to a file, you need to include the _____ header file in your program
file.h
fstream.h
iomanip.h
iostream.h
of stream.h
Answer: Option B

23. 
A C++ _____ is a program that runs in a DOS window
Algorithm
cast application
console application
source application
Answer: Option C

24. 
A difference, between reference variables and pointers is that
reference variables are easier to use
pointers are easier to use
reference variables are more flexible
no difference exists between reference variables and pointers
Answer: Option A



25. 
A 'C function does not contain
a function header
argument declarations
other 'C' functions
function body
None of the above
Answer: Option C

Thursday, 16 May 2013

Find output

#include <stdio.h>
int main()
{
int mar[5]={0,1,2,3,4};
int j,m,i;
i=++mar[0];
j=mar[1]++;
m=mar[i++];
printf("%d %d %d\n\n", i,j,m);
return 0;
}


Answer: i=2, because you increment it in m=mar[i++], j = 1, because the increment carried out after the assignment and m=2, because m=mar[2] => 2 1 2