Motorola Previous Years Solved Sample Placement Papers
-
What is the difference between Console.Write() and Console.WriteLine() methods in C#?
A: Write() writes a single character while Console.WriteLine() writes a complete line
B: Write() writes the data on the console without printing a newline while Console.WriteLine() writes the data on the console along with printing a newline
C: Write() writes the string on the console while Console.WriteLine() writes the string as well as values of the variables on the console
D: Both can be used for the same purpose
Ans: BExplanation:
The only difference between the Console.Write() and Console.WriteLine() is that the Console.Write() is used to write data without printing the new line, while Console.WriteLine() is used to write data along with printing the new line.
-
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { int a = 10, b = 20; Console.WriteLine("{0},{0}", a, b); } } }
A: 10,10
B: 10,20
C: 20,20
D: Error
Ans: AExplanation:
In the above code, there are two variables a and b, but while printing the values of a and b, we are using the same placeholder
{0}
, that will print the value of the first variable. Thus, the output is 10,10. -
What will be the output of the following C# code?
using System; namespace MyApplication { class Program { static void Main(string[] args) { int a = 10, b = 20; Console.WriteLine("{0}+{1}", a, b); } } }
A: 20
B: 30
C: 10+20
D: 10+10
Ans: CExplanation:
In the statement,
Console.WriteLine("{0}+{1}", a, b);
-{0}
is the placeholder for variablea
and{1}
is the placeholder for variableb
.{0}+{1}
will not perform any operation; values ofa
andb
will be printed at the place of{0}
and{1}
. Thus, the output will be 10+20. -
What will be the output of the following C# code?
using System; class Program { static void Main(string[] args) { int i = 2; int j = 10 / 4; if (i == j) { Console.WriteLine("True"); } else { Console.WriteLine("False"); } } }
A: True
B: False
C: Error
D: None
Ans: AExplanation:
The statement
int j = 10 / 4;
results inj
being assigned the value 2 due to implicit type casting. Thus, the output will be "True". -
What will be the output of the following C# code?
using System; class Program { static void Main(string[] args) { Console.WriteLine(true ^ true); } }
A: True
B: False
C: Error
D: None
Ans: BExplanation:
Here, we are using the Logical exclusive OR operator
^
(XOR). It returns true when one operand evaluates to true and the other evaluates to false. In the above code, both operands are true. Thus, the output will be False. -
What will be the output of the following C# code?
using System; class Program { static void Main(string[] args) { Console.WriteLine(true && false); } }
A: True
B: False
C: Error
D: None
Ans: BExplanation:
Here, we are using the Conditional logical AND operator
&&
. It returns true only if both operands evaluate to true. In the above code, the operands are true and false. Thus, the output will be "False".