Sunday, 15 December 2024

Programming Based Questions Class X ICSE - Computer Applications

 ICSE Computer Applications 

Programs Based Questions

Class X

[CONSTRUCTORS AND METHODS ]

Board Paper Section A  Question 2:                                                                                   [10 Marks]

[A] Read the code segment and answer the quetsions that follow: 

(I)  

private int x;                /* Private Member Variable*/ 

public GetOutput(short a)    //Constructor Definition

{

      x=++a + --a + ++a;

      System.out.println(x);

 }

public void func()

{


      GetOutput obj=new GetOutput(4);

                                       /* Assume main method call func()*/

}

(i) Name the class in which above code may be defined

(ii) What value will the variable a store when the object is generated.

(iii) What value will a store after the code gets executed.

(iv) What will be the output if the above code is executed assuming that method func() is called.


Solution:- 

(i) GetOutput

(ii) a will be store 4 at the time of object creation the constructor get invoked.

(iii) After the method GetOutput gets executed, a=5.

(iv) 14

(II)  

private boolean var;                

private int value;

public Sample(boolean m, int var)

{

    var=m;

    value=2*val-- + --val;

}

public void func()

{

    int k=4; bolean b=false;

    Sample s=new Sample(b, k);

}

(i) Name the class in which above code is defined

(ii)  Name the formal parameter in the code.

(iii) Name the actual parameter in the code.

(iv) Will the Sample() constructor get invoked if an object of same class is created in another class MyWorld?

Give Reason.

(v) What is the result stored in the value ? 

Solution:- 

(i) Sample

(ii) m and var are formal parameter.

(iii) b and k are actual parameter  

(iv) Yes, the constructor will get invoked because it has been specified with public access.

(v) value stored 10.

     2x4+2=10 (val is determined by 1 twice)

 

 (III) 

class Sum

{

     private int x, y;

     public total;

     public Sum(int a, int b)

     {

              x=a;

              y=b;

              total=x+y;  

      }

public static void main(String args[])

{

     Sum tot=new Sum(50, 46)

     System.out.println("Total of 50 and 46 is : "+ tot.total);

}

(i) Write all data memebres of classSum. 

(ii)  Write all member methods of the class Sum.

(iii) Can you access variables x and y in its public static void main() as follows:

     System.out.println("Total of 50 and 46 is : "+ tot.total);

Give reason for your answer. 

(iv) Can you access x and y in another class MyAccount's public static void main as follows:

     System.out.println("x is ' +x);

     System.out.println("y is ' +y);

Write the statement if there is any other way to access it.

(v) In the above code, how many times will Sum() get invoked.

Solution:-

(i) Data member of class Sum : x, y , total.

(ii) Member Methods : Sum (constructor) and main()

(iii) Yes, variable x and y are accessible in public static void main() as given through thay are declared private. Here main()  is defined in the same class of which x and yare members. Private members are accessible in all member methods of the same class. So the given statement is valid.

(iv) There's no way by which x and y can be accessed in any other calss till they are decalred as private. If theaccess specifiers of x and y is changed to public, then thay can be accessed in the public static void main method of MyAccount class in the following way:

class MyAccount

{

        public static void main(String args[])

        {

             Sum obj=new Sum();

             obj.x=10;

             obj.y=20;

             System.out.println("x is :"+x);

             System.out.println("y is :"+y);

        }

}

(v) Sum constructor will get invoked only once, as only one object tot is created in the given code.



No comments:

Post a Comment

CLASS X TYPES OF NUMBERS

 CLASS X  TYPES OF NUMBERS List of types of Numbers 1. Arm Strong Number 2.  Co-prime Number 3. Duck Number [I.C.S.E 2024] 4. Magic Number  ...