Showing posts with label Output Questions. Show all posts
Showing posts with label Output Questions. Show all posts

Sunday, 14 February 2016

Programming Output(C++)

1. Predict the output of the following code:

#include<iostream>
using namespace std;
class A
{
    public:
        A(int n)
        {
            cout<<n<<endl;
        }
};

class B:public A
{
    public:
        B(int n,double m):A(n)
        {
            cout<<m<<endl;
        }
};

class C:public B
{
    public:
        C(int n,double m,char o):B(n,m)
        {
            cout<<o<<endl;
        }
};

int main()
{
    C object(12,12.21,'s');
    return 0;
}








Answer:
12
12.21
s


Thursday, 21 January 2016

Programs Output(2)

1. Find the output of the following program

#include<stdio.h>
#include<conio.h>
void main(void)
{
    char str[]={"\0play\nhard"};
    printf("%%d %d\n",sizeof(str));
    printf("%s",str);
    getch();
}

Output:--
 printf("%s",str) statement prints NULL because str has NULL as it's first character.










2. What would be the output of the following program ?

#include<stdio.h>
#include<conio.h>
main()
{
    long i=2;
    int j=2;
    float k=2.1;
    double l=2.1;
    if(i==j)
    printf("A");
    else
    printf("B");
    if(k==l)
    printf("C");
    else
    printf("D");
}

OUTPUT:--
 long i=2; is same as long int i=2; therefore i==j is TRUE. While in floating point numbers/variables with relational operators we need to take care, because in the case of floating point numbers the values cannot be predicted easily. This is on account of the number of bytes; the precision varies with the number of bytes. As we know float takes 4 bytes and long double takes 10 bytes. Therefore float stores 0.9 with less precision than long double.

Friday, 15 January 2016

Programming Output Questions in C

1. Find the output of the following code:

#include<stdio.h>
main()
{
  int variable1=1;
  int variable2=12;
  int variable3=12;
  variable1=variable2==variable3;
  printf("%d",variable1);
}

OUTPUT:   1
12==12 implies it's true , therefore variable1=1


2. Find the output of the following code:

#include<stdio.h>
main()
{
  char *pointer;
  printf("%d %d",sizeof(*pointer),sizeof(pointer));
}

OUTPUT:   1   2
sizeof(pointer) gives the value 2 because pointer is a character pointer which needs two bytes
to store the address while storing the value of a character requires 1 byte.


3. Find the output of the following code:

#include<stdio.h>
main()
{
  int a=7;
  printf("%d",a=++a==8)
}

OUTPUT   1
The resolved expression is a=(++a==8) , therefore the inner expression results in TRUE i.e. 1.

4. Find the output of the following code:

#include<stdio.h>
#include<conio.h>
main()
{
    int *m,n[10];
    printf("Enter the values of m and n \n");
    scanf("%d \t %d",&m,&n);
    //  Suppose m=5 and n=10
    printf("%d \t %d",m,n);
}

 OUTPUT   5      garbage value
m is a pointer variable and it's value is 5 which is printed as it is
n is an array, it prints garbage value