Professional Documents
Culture Documents
MCQ Java 1 Discussion
MCQ Java 1 Discussion
=======================================
Q4. What will be the result of compiling and running the following code?
class Base{
public short getValue(){ return 1; } //1
}
class Base2 extends Base{
public byte getValue(){ return 2; } //2
}
public class TestClass{
public static void main(String[] args){
Base b = new Base2();
System.out.println(b.getValue()); //3
}
}
Select 1 option
A. It will print 1
B. It will print 2.
C. Compile time error at //1
D. Compile time error at //2
Select 1 option
A. public class ImaginaryNumber extends Number {
}
B. public class ThreeWayBoolean extends Boolean {
}
C. public class NewSystem extends System {
}
D. public class ReverseString extends String {
}
Q6. class A {
public static void f(){
System.out.println(“fA”); }}
class B extends A{
public void f(){
System.out.println(“fB”); }
public static void main(String[] args) {
A a= new B();
a.f(); }}
What will happen on compilation or execution of code?
A. fA
B. fB
C. Code will not compile
D. Code will throw runtime error
Q7. What will be the result of attempting to compile and run the following program?
public class TestClass{
public static void main(String args[ ] ){
A o1 = new C( );
B o2 = (B) o1;
System.out.println(o1.m1( ) );
System.out.println(o2.i );
}
}
class OverrideTest {
public static void main(String[] args) {
Base bObj = new Derived();
bObj.foo(bObj);
}
}
What is the output of this program when executed?
a)
In Base.foo()
In Base.bar()
b)
In Base.foo()
In Derived.bar()
c)
In Derived.foo()
In Base.bar()
d)
In Derived.foo()
In Derived.bar()
Q14. . Given:
class Rocket {
private void blastOff() { System.out.print("bang "); }
}
public class Shuttle extends Rocket {
public static void main(String[] args) {
new Shuttle().go();
}
void go() {
blastOff();
// Rocket.blastOff(); // line A
}
private void blastOff() { System.out.print("sh-bang "); }
}
Which are true?
A. As the code stands, the output is bang
B. As the code stands, the output is sh-bang
C. As the code stands, compilation fails.
D. If line A is uncommented, the output is bang bang
System.out.println(T.temp);
}
}
Demo(){
System.out.println("inside ctr");
}
Demo demo=new Demo();
public static void main(String[] args) {
new Demo();
}
}
}
What is the o/p?
------------------
public class Demo {
Integer i1=127;
Integer i2=127;
System.out.println(i1==i2);
Integer i3=128;
Integer i4=128;
System.out.println(i3==i4);
}
}
// Integer.valueOf();
Integer cache ( for performance)
class A{
public static void foo(){
System.out.println(" i am static method");
}
}
public class Demo {
A a=null;
a.foo();
}
Which code with compile?
-----------------------------
System.out.println(s2);
System.out.println(returnSomething());
=>maigic of finally
=>never ever return value from finally block ? why?
class Cat{
public static void meow(){
System.out.println("meow");
}
}
c.meow();
}
}
What is the O/P?
-----------------
class Cat{
public static String foo="value";
}
System.out.println(new CostlyCat().foo);
}
}
new Confusion(null);
double d=1.1-1.00;
if(d==0.10)
System.out.println("hello");
else
System.out.println("hi");
Why?
What to do?
---------
=> if you want to manipulate double never use double / float
use BigDecimal class
Correct approach?
----------------
BigDecimal d2=new BigDecimal("1.10").subtract(new BigDecimal("1.00"));
System.out.println(d2);
if(d2.doubleValue()==0.10)
System.out.println("hello");
else
System.out.println("hi");
What is the op?
------------------
class Foo {
private int i;
public Foo(int i) {
this.i = i;
}
System.out.println(f1==f2);
StringBuffer word=null;
switch (random.nextInt(5)) {
case 1:
word=new StringBuffer('M');
break;
default:
word=new StringBuffer('N');
}
word.append('a');
word.append('b');
System.out.println(word);
}
class String {