AmbitionBox

Discover Best Places to work in India

C# Programming

C# Programming Questions and Answers

Showing 131 - 140 of 174 questions

131

Consider a class maths and we had a property called as sum.b is a reference to a maths object and we want the statement b.sum = 10 to fail.Which of the follwing is the correct
solution to ensure this functionality?

a

Declare sum property with both get and set accessors

b

Declare sum property with both get and set accessors

c

Declare sum property with get, set and normal accessors

d

None of the mentioned

correct answer c

Answer: Option (C)

132

Select the correct statement about properties of read and write in C#.NET?

a

A property can simultaneously be read or write only

b

A property can be either read only or write only

c

A write only property will only have get accessor

d

A read only property will only have get accessor

correct answer b,d

Answer: Option (B) and (D)

133

Select output for the following set of code:

class sample

{

     publicint i;

     publicint[] arr =newint[10];

     publicvoid fun(int i, int val)

     {

          arr[i]= val;

     }

}

class Program

{

     staticvoid Main(string[] args)

     {

          sample s =new sample();

          s.i=10;

          sample.fun(1, 5);

          s.fun(1, 5);

          Console.ReadLine();

     }

}

 

 

 

 

a

sample.fun(1, 5) will not work correctly

b

s.i = 10 cannot work as i is ‘public’

c

sample.fun(1, 5) will set value as 5 in arr[1] d) s.fun(1, 5) will work correctly

d

None of the mentioned

correct answer a

An Object reference is required for non static field,method or property.
i.e sample s = new sample();
s.i = 10;
sample.fun(1, 5);
sample.fun(1, 5);
Console.ReadLine();

134

The output of code is ?

class test

{

     publicvoid print()

     {

          Console.WriteLine("Csharp:");

     }

}

class Program

{

     staticvoid Main(string[] args)

     {

          test t;

          t.print();

          Console.ReadLine();

     }

}

 

 

a

Code runs successfully prints nothing

b

Code runs and prints “Csharp”

c

Syntax error as t is unassigned variable which is never used

d

None of above mentioned

correct answer c

object of class test should be declared as test t = new test();
test t = new test();
t.print();
Console.ReadLine();

135

The operator used to access member function of a class?

 

a

:

b

::

c

.

d

#

correct answer c

objectname.function name(actual arguments);

136

Select the output for the following set of code :

class sample

{

     publicint i;

     publicint j;

     publicvoid fun(int i, int j)

     {

          this.i= i;

          this.j= j;

     }

}

class Program

{

     staticvoid Main(string[] args)

     {

          sample s =new sample();

          s.i=1;

          s.j=2;

          s.fun(s.i, s.j);

          Console.WriteLine(s.i+" "+ s.j);

          Console.ReadLine();

     }

}

 

 

 

 

 

 

 

a

Error while calling s.fun() due to inaccessible level

b

Error as ‘this’ reference would not be able to call ‘i’ and ‘j’

c

1 2

d

Runs successfully but prints nothing

correct answer c

Variable ‘i’ and ‘j’ declared with scope public in sample class are accessed using object of class ‘sample’ which is ‘s’.
Output:1 2.

137

What will be the output of the following snippet of code?

class number

{

     int length =50;

     publicint number1

     {

          get

          {

               return length;

          }

          set

          {

               length =value;

          }

     }

}

class Program

{

     publicstaticvoid Main(string[] args)

     {

          number p =new number();

          p.number1= p.number1+40;

          int k = p.number1*3/9;

          Console.WriteLine(k);

          Console.ReadLine();

     }

}

 

 

 

 

a

0

b

180

c

30

d

Compile time error

correct answer c

Answer: Option (C)

138

Choose the correct statement among the following?

a

A property can be a static member whereas an indexer is always an instance member

b

A get accessor of a property corresponds to a method with no parameters whereas get accessor of an indexer corresponds to a method with the same formal parameters lists as the indexer

c

It is an error for indexer to declare a local variable with the same name as indexer parameters

d

All of the mentioned

correct answer d

Answer: Option (D)

139

Choose the keyword which declares the indexer?

a

base

b

this

c

super

d

extract

correct answer b

The indexer is declared using the name this.

140

Choose the operator/operators which is/are used to access the [] operator in indexers?

a

get

b

set

c

access

d

All of the mentioned

correct answer a,b

The indexer is implemented through the get and set accessors for the [] operator as:
public double this[int idx] {
get
{
if()
{
}
else
{
return([idx]);
}

 

}
set
{
array[idx];

}
}

Select a company to compare with

vs

Similar Companies