AmbitionBox
Discover Best Places to work in India
Discover best places to work
Compare & find best workplace
Bring your workplace to life
Highlight your company's perks
Read reviews for 6L+ companies
Rate your former or current company
Discover salaries for 8L+ companies
Calculate your take home salary
Check your market value
Help other jobseekers
Read interviews for 90K+ companies
Interviews questions for 1K+ colleges
Contribute your interview questions
C# Programming
Correct Declaration of Values to variables ‘a’ and ‘b’?
int a = 32, b = 40.6
int a = 42; b = 40
int a = 32; int b = 40
int a = b = 42
a) Although,declaration of ‘b’ and ‘a’ are correct but initialization of value to ‘b’ should be ‘int’ datatype not float. b) Missing declaration type of ‘b’. c) correctly declared datatypes ‘a’ and ‘b’. d) ‘b’ can’t be assigned values before declaration.
Select error in the given program :
1.
static Void Main(String[] args)
2.
{
3.
constint m =100;
4.
int n =10;
5.
constint k = n /5*100* n ;
6.
Console.WriteLine(m * k);
7.
Console.ReadLine();
8.
}
‘k’ should not be declared constant
Expression assigned to ‘k’ should be constant in nature
Expression (m * k) is invalid
m ‘ is declared in invalid format
’k’ should be declared as const int k = 10/5 * 100*10 i.e only constant values should be assigned to a constant. Output :Error 1 – The expression being assigned to ‘k’ must be constant.
Correct output for code is?
staticvoid Main(string[] args)
float a = 10.553f;
long b = 12L;
int c;
c = Convert.ToInt32(a + b);
Console.WriteLine(c);
23.453
22
23
22.453
The two datatype ‘float’ and ‘long’ after arithmetic operation completely converted to nearest whole number 23. Output : 23.
Select the output for the relevant code set:
int a =4, b =5, c =7, u =9;
int h;
h =(Convert.ToInt32(u < b))+(a + b--)+2;
Console.WriteLine(h);
Console.WriteLine(b);
Console.WriteLine(u < b);
9.
12,15,0
11,4,False
11,15,0
12,4,False
Step 1: Convert.ToInt32(u < b)(Evaluate result as 9 < 5 which is false in nature.So, solution is converted from 'false' to '0').
Step 2: (a + b--) evaluated as 4 + 5 = 9 + 2 =11. Step 3: u < b evaluated as 'False' without being converted to '0'.
Output : 11 4 False
What is the output of the following code?
static void Main(string[] args)
int a = 5 ;
if(Convert.ToBoolean((.002f)-(0.1f)))
Console.WriteLine("Sachin Tendulkar");
elseif(a ==5)
Console.WriteLine("Rahul Dravid");
else
Console.WriteLine("Ms Dhoni");
Rahul Dravid
Sachin Tendulkar
MS Dhoni
Warning : Unrechable code
(0.002 – 0.1f) not equivalent to zero hence it is true. So,only first if clause will execute and print:Sachin Tendulkar on console.As,first condition is always true so no else if statement will be executed. Output: Sachin Tendulkar
Select the correct ‘if statement’ to be filled in the given set of code :
static void Main(string [] args)
int[]num ={50, 65, 56, 88, 43, 52};
int even =0, odd =0;
for(int i =0;i < num.Length;i++)
/*_________________*/
Console.WriteLine("Even Numbers:"+even);
Console.WriteLine("Odd Numbers:"+odd);
if ((num % 2) == 0)
even += 1;
odd += 1;
if((num * i) == 0) { even += 1; } else { odd += 1; }
if(num[i] % 2 == 0) { even += 1; } else { odd += 1; }
if(num[i] % 2 = 0) { even += 1; } else { odd += 1; }
int []num = {50, 65, 56, 88, 43, 52}; int even = 0,odd = 0; for (int i = 0 ;i < num.Length ;i++) { if (num[i] % 2 == 0) { even += 1; } else { odd += 1; } } Console.WriteLine("Even Numbers: " +even); Console.WriteLine("Odd Numbers: " +odd); Console.ReadLine()
Select a convenient declaration and initialization of a floating point number:
float somevariable = 12.502D
float somevariable = (Double) 12.502D
float somevariable = (float) 12.502D
float somevariable = (Decimal)12.502D
We cannot implicitly convert a “double” number directly to any other datatype.Here, its float we have to add the required datatype to number as : float somevariable = (float)12.502D; or Double somevariable = (Double)12.502D;
Correct way to define a value 6.28 in a variable ‘a’ where value cannot be modified ?
#define a 6.28F
pi = 6.28F
const float pi = 6.28F
const float pi
Const is a reserve keyword whenever they are declared with any variables.The value stored in that variable always remain fixed.Any modification done to change value of that variable results in error.Hence, options a, b and d are rejected because value is not declared fixed.Now, for value ‘c’ and ‘d’ only option ‘c’ is correct because for value ‘d’ while declaring a constant variable we need to provide a constant value too as provided in ‘c’. So, option ‘c’ is correct way of declaration of constant variable.
Why does a float variable stop incrementing at number ‘16777216’ in given code in C#?
float = 0 ;
while(true)
a++;
if ( a > 16777216 )
break;
Sign and Exponent for ‘16777217’ is same as for ‘16777216’
Mantissa is different for ‘16777216’ and ‘16777217’
Sign and Exponent for ‘16777217’ is different from ‘16777216’
None of the mentioned
16777216 is exactly 224, and would be represented as 32-bit float like so: sign = 0 (positive number) exponent = 24 (stored as 24 + 127 = 151 = 10010111) mantissa = . 0 As 32 bits floating-point representation: 0 10010111 00000000000000000000000 Therefore: Value = (+ 1) * 2 ^ 24 * (1. 0 + . 0) = 2 ^ 24 = 16777216 Now let’s look at the number 16777217, or exactly 224 + 1: sign and exponent are the same. Mantissa should have to be exactly 2-24 so that (+ 1) * 2 ^ 24 * (1. 0 + 2 ^-24) = 2 ^ 24 + 1 = 16777217 and here lies the actual problem . The mantissa cannot have the value 2-24 because it only has 23 bits, so the number 16777217 just cannot be represented with the accuracy of 32-bit floating points numbers.
Select the output for the following set of Code :
int a =8, b =6, c =10;
int d = a * c *2/ Convert.ToInt32(Math.Pow((c - b), 2));
if(d ==(c = Convert.ToInt32(Math.Sqrt(a * a + b * b)))&& c ==10)
Console.WriteLine("figure is hypotenuse");
Console.WriteLine("figure is square");
Figure is square
Figure is hypotenuse
False
Solving the expression for ‘c’ we get c==10 in if first condition as (c == Convert.ToInt32(Math.Sqrt(a * a + b * b))). The logical condition when d == (c = 10) suits here . Similarly, going for second condition where c ==10 as ‘&&’ operator exists between both given condition and at last both are evaluated to true as c == 10. So, only first statement is executed. Output :Figure is square
Join India’s largest community to research company culture
Are you a student or working professional?
Student/Never worked
I am a student/I have never worked
Working Professional
I am working/I have worked before
What are your preferred job locations?
Popular Cities
Other Cities
Follow your preferred designations/job profiles
Suggestions based on your job profile
vs
Similar Companies