Friday 7 June 2013

Loop Question 6

What will be output of following c code?

#include<stdio.h>
int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
         printf("%d ",i);
         if(i!=1) break;
    }
    return 0;
}



EXPLANATION


Output: -1
Explanation:
Initial value of variable i is 1.
First iteration:
For loop initial value: i = 0
For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1
Since variable i is not equal to 1. So, if condition is true. Due to break keyword program control will come out of the for loop.

Loop Question 5

What will be output of following c code?



#include<stdio.h>
int main(){
    static int i;
    for(++i;++i;++i) {
         printf("%d ",i);
         if(i==4) break;
    }
    return 0;
}



EXPLANATION


Output: 24
Explanation:
Default value of static int variable in c is zero. So, initial value of variable i = 0
First iteration:
For loop starts value: ++i i.e. i = 0 + 1 = 1
For loop condition: ++i i.e. i = 1 + 1 = 2 i.e. loop condition is true. Hence printf statement will print 2
Loop incrimination: ++I i.e. i = 2 + 1 =3
Second iteration:
For loop condition: ++i i.e. i = 3 + 1 = 4 i.e. loop condition is true. Hence printf statement will print 4.
Since is equal to for so if condition is also true. But due to break keyword program control will come out of the for loop.

Loop Question 4

What will be output of following c code?


#include<stdio.h>
int main(){
    int i,j;
    i=j=2,3;
    while(--i&&j++)
         printf("%d %d",i,j);
    return 0;
}




EXPLANATION


Output: 13
Explanation:
Initial value of variable
i = 2
j = 2
Consider the while condition : --i && j++
In first iteration:
--i && j++
= 1 && 2 //In c any non-zero number represents true.
= 1 (True)
So while loop condition is true. Hence printf function will print value of i = 1 and j = 3 (Due to post increment operator)
In second iteration:
--i && j++
= 0 && 3  //In c zero represents false
= 0  //False
So while loop condition is false. Hence program control will come out of the for loop.

Loop Question 3


What will be output of following c code?

#include<stdio.h>
int main(){
    int x=011,i;
    for(i=0;i<x;i+=3){
         printf("Start ");
         continue;
         printf("End");
    }
    return 0;
}



EXPLANATION


Output: Start Start Start
Explantion:
011 is octal number. Its equivalent decimal value is 9.
So, x = 9
First iteration:
i = 0
i < x i.e. 0 < 9  i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 3
Second iteration:
i = 3
i < x i.e. 3 < 9 i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 6
Third iteration:
i = 3
i < x i.e. 6 < 9 i.e. if loop condition is true.
Hence printf statement will print: Start
Due to continue keyword program control will come at the beginning of the for loop and value of variable i will be:
i += 3
i = i + 3 = 9
fourth iteration:
i = 6
i < x i.e. 9 < 9 i.e. if loop condition is false.
Hence program control will come out of the for loop.

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