Q:

C#.Net find output programs (Boxing & Unboxing) | set 1

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Boxing & Unboxing | Set 1: Enhance the knowledge of C#.Net Boxing & Unboxing concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int num = 10;

        object ob = num;

        num = 20;

        Console.WriteLine(num);
        Console.WriteLine(ob);
    }
}

Question 2:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        object B =A;

        A = 20;

        C = A + B;

        Console.WriteLine(C);
    }
}

Question 3:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        Integer B =A;

        A = 20;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

Question 4:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        int A = 10;
        int C = 0;

        Object B =A;

        A = 20;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

Question 5:

using System;

class Program
{
    //Entry point of the program
    static void Main(string[] args)
    {
        const int A = 10;
        int C = 0;

        object B =A;

        B = 50;

        C = A + (int)B;

        Console.WriteLine(C);
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

Output:

20
10
Press any key to continue . . .

Explanation:

In the above program, we created a local variable num initialized with 10.

object ob = num;

In the above statement, we perform boxing of variable num and assigned to object ob, and then we assigned the value 20 into variable num, and then print the value of num and ob on the console screen.

Answer  2:

Output:

main.cs(15,13): error CS0019: Operator `+' cannot be applied to 
operands of type `int' and `object'

Explanation:

The above program will generate a compile-time error because here B an object and A an integer variable that cannot be added directly, we need to typecast the value of object B.

Answer 3:

Output:

main.cs(11,9): error CS0246: The type or namespace name `Integer' 
could not be found. Are you missing an assembly reference?
main.cs(15,22): error CS0841: A local variable `B' 
cannot be used before it is declared

Explanation:

The above program will generate syntax error because Integer is not any type or object in C#.

Answer 4:

Output:

30
Press any key to continue . . .

Explanation:

In the above program, we created two local variables A and C initialized with 10 and 0 respectively.

Object B =A;

In the above statement, we box the value of A into object B.

C = A + (int)B;

In the above statement, we un-boxed the value of object B and added to the variable A and assigned to the variable C. Then finally print the value of variable C on the console screen.

Answer 5:

Output:

60
Press any key to continue . . .

Explanation:

In the above program, we created a constant A and variable C initialized with 10 and 0 respectively.

object B =A;

In the above statement, we boxed the value of A into object B.

C = A + (int)B;

In the above statement, we un-boxed the value of object B and added to the variable A and assigned to the variable C. Then finally print the value of variable C on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Structure) | set 1... >>
<< C#.Net find output programs (Enumeration) | set 2...