Motorola Previous Years Solved Sample Placement Papers
-
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { bool x = true; Console.Write(Convert.ToString(x)); } } }
A: True
B: true
C: False
D: false
Ans: A) TrueExplanation: In the above C# code, we are using
Convert.ToString()
method which converts abool
tostring
. Thus, the output will be "True". -
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { double x = 10.25; Console.Write(Convert.ToInt32(x)); } } }
A: 10.30
B: 10.25
C: 10
D: Error
Ans: C) 10Explanation: In the above C# code, we are using
Convert.ToInt32()
method which converts adouble
toint
. Thus, the output will be "10". -
What is 'Console' in C#?
A: Class
B: Object
C: Method
D: Structure
Ans: A) ClassExplanation: In C#, the
Console
class is used to represent the standard input, output, and error streams for console applications. -
What will be the output of the following C# code, if the input is 123?
using System; namespace MyApplication { class Program { static void Main(string[] args) { Console.WriteLine("Enter a number:"); int num = Console.ReadLine(); Console.WriteLine("Given number is: " + num); } } }
A: Given number is:123
B: Given number is: 123
C: Given number is: "123"
D: Error
Ans: D) ErrorExplanation: In C#,
Console.ReadLine()
is used to read astring
. Here, we are trying to assign the input to anint
variable directly, which causes an error. The error is:Cannot implicitly convert type 'string' to 'int'
. -
What will be the output of the following C# code, if the input is 123?
using System; namespace MyApplication { class Program { static void Main(string[] args) { Console.WriteLine("Enter a number:"); int num = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Given number is: " + num); } } }
A: Given number is:123
B: Given number is: 123
C: Given number is: "123"
D: Error
Ans: B) Given number is: 123Explanation: In C#,
Console.ReadLine()
reads input as astring
. UsingConvert.ToInt32()
, we convert the input to anint
, so there is no error. Output will be:Given number is: 123
. -
Which is/are the correct method(s) to input a float value in C#?
A:
Parse(Console.ReadLine())
B:
ToSingle(Console.ReadLine())
C:
ToFloat(Console.ReadLine())
D: Both A and B
E: Both A and C
Ans: D) Both A and BExplanation: In C#, the following methods can be used to input a float value:
float.Parse(Console.ReadLine())
: Parses a string to a float.Convert.ToSingle(Console.ReadLine())
: Converts a string to a single-precision floating-point number.
ToFloat(Console.ReadLine())
is not a valid method in C#.