Monday, 19 October 2015

java quiz-3

java quiz-3



1
suppose you have  taken input as x=96 y=96.35 and s=java
what will be the output?
import java.util.Scanner;

public class Solution
{
public static void main(String[] args)
{
            Scanner sc=new Scanner(System.in);
            int x=sc.nextInt();
            double y=sc.nextDouble();
            String s=sc.nextLine();
            System.out.print(x);
            System.out.print(y);
            System.out.print(s);
}
}

9696.35java
96 96.35 java
CompileError
9696.35

2
class Wrap
{
public static void main(String args[])
{
Boolean b1=new Boolean("java");
System.out.println(b1);
}
}
java
false
true
compileError


3
class String_Check
{
public static void main(String args[])
{
String s1=new String();
System.out.print(s1);
}
}
null
no output
String_Check@15db9742
compile error


4
class String_Check
{
public static void main(String args[])
{
String s3=null;
String s4=null;
System.out.println(s3.equals(s4));
}
}
null
true
false
a compile time exception


5
public class B extends A
{
private int bar;
public void setBar(int b)
{
bar = b;
}
}
class A {
public int foo;
}
class A is tightly encapsulated
class B is tightly encapsulated
class A and B is tightly encapsulated
neither class A nor class B is tightly encapsulated


6
class A
{
public void baz()
{
System.out.println("A");
}
}
class B extends A
{
public static void main(String [] args)
{
A a = new B();
a.baz();
}
public void baz() {
System.out.println("B");
}
}
A
B
compilation error
runtime exception


7
class ThreeConst
{
public static void main(String [] args)
{
new ThreeConst(4L);
}
public ThreeConst(int x) {
this();
System.out.print(" " + (x * 2));
}
public ThreeConst(long x) {
this((int) x);
System.out.print(" " + x);
}
public ThreeConst()
{
 System.out.print("no-arg ");
}
}
4
8 4 no-arg
no-arg 8 4
compile time error


8
int x;
x=n.test();
int test()
{
//line x
return y;
}
which line of code,inserted at line x,will not compile?
short y=7;
int y=(int)7.2d;
Byte y=7;
int y=0xface;


9
class Test
{
public static Foo f = new Foo();
 public static Foo f2;
 public static Bar b = new Bar();

 public static void main(String [] args)
 {
 for (int x=0; x<6; x++)
{
 f2 = getFoo(x);
 f2.react();
 }
 }
 static Foo getFoo(int y)
 {
 if ( 0 == y % 2 )
 {
 return f;
 }
else
{
 return b;
 }
 }
 }
 class Bar extends Foo
 {
 void react()
 {
 System.out.print("Bar "); }
 }
 class Foo {
 void react() { System.out.print("Foo "); }

 }
Bar Bar Bar Bar Bar Bar
Foo Bar Foo Bar Foo Bar
Foo Foo Foo Foo Foo Foo.
Compile error.


10
class Foo
{
String doStuff(int x) { return "hello"; }

}
which method would not be legal in a subclass of Foo?
 String doStuff(int  x) { return "hello"; }
 int doStuff(int x) { return 42; }
 public String doStuff(int x) { return "Hello"; }
 protected String doStuff(int x) { return "Hello"; }

No comments:

Post a Comment