JavaTconnect
Home
HackerRank Solutions
Projects
Quiz
Contact Us
Wednesday, 9 September 2015
java quiz-1
1
public static void main(String[] args)
{
byte b1= 25; byte b2=45;
byte b3= b1+b2;
}
25
70
CompileError
RunTimeException
2
Consider the following command-line invocations?
i. java Arrays
ii. java Arrays 12
iii. java Arrays 12 32
class Arrays
{
public static void main(String [ ] args)
{
for(int x=0;args.length>x++;)
{
System.out.print(args[x]+ " ");
}
Only the invocation i will complete without throwing exceptions
Only Invocation i will throw an exception.
Invocation ii will produce he output 12.
Invocation iii will produce he output 12 32.
3
What is the result?
public static void main(String[] args)
{
Object obj = new int[] { 1, 2, 3 };
int[] someArray = (int[])obj; // line 13
for (int i : someArray)
System.out.print(i + " "); // line 14
}
1 2 3
Compilation fails because of an error in line 12.
Compilation fails because of an error in line 13.
Compilation fails because of an error in line 14.
4
What is the result of running the following code with "java Test debug":
class Test
{
public static void main(String [ ] args)
{
if (args.length == 1 | args[1].equals("debug"))
{
System.out.println(args[0]);
}
else
{
System.out.println("Release");
}
}
}
Debug
Release
Compilation fails
An exception is thrown at run-time
5
public class LoopTest
{
public static void main(String args[])
{
int a = 15;
outside: for (int i = 0; i<3 4="" br="" i="" line=""> inside: System.out.print(i); // line 5
for (int j = 1; j<3 br="" j=""> if (a>5)
continue inside;//line 8
break outside; // line 9
}
}
}
}
Compile Error at line 5
Runtime exception is thrown at line 8
Compile Error at line 9
Compile Error at line 8
6
public class Tester
{
public static void main(String[] args)
{
do
{
System.out.print("inside do");
} while (false);
while (false)
{
System.out.print("inside while");
}
System.out.print("outside");
}
}
inside do outside
inside do inside while outside
outside
compile error
7
which of the followings are INCORRECT when you try to compile and execute the below code ?
class s
{
public static void main(String agr[])
{
short s1=4; //LINE 1
short s2 = s1+=s1; //LINE 2
short s3= s1+s2; //LINE 3
byte b2=(byte)((byte)s1 +(byte)(byte)s2); //LINE 5
}
}
compile time error at LINE 3
compile time error at LINE 2
compile time error at LINE 1
compile time error at LINE 5
8
class Test
{
public static void main(String[] args)
{
float f = 1; // line 1
System.out.println(++f); // line 2
}
}
compilation error in line 1
compilation error in line 2
2.0
Runtime exception
9
public class Tester {
static void test(float x) {
System.out.print("float");
}
static void test(double x) {
System.out.print("double");
}
public static void main(String[] args) {
test(99.9);
}
}
float
double
Compilation error
Exception is thrown at run time
10
if byte b=50;
b=b*2;
error ! can't assign an int to a byte
b=100
b=50
b=100.0
3>3>
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment