Sunteți pe pagina 1din 6

Assignment 3

___________________________________________________________________________
1. class Mixer { Mixer() { } Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String[] args) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go();} void go() { System.out.print("hi "); } } What is the result of attempting to compile and run the above program? A. hi hi hi B. Compile-time error C. hi followed by an Exception D. hi hi followed by an Exception class Fizz { int x = 5; public static void main(String[] args) { final Fizz f1 = new Fizz(); Fizz f2 = new Fizz(); Fizz f3 = FizzSwitch(f1,f2); System.out.println((f1 == f3) + " " + (f1.x == f3.x)); } static Fizz FizzSwitch(Fizz x, Fizz y) { final Fizz z = x; z.x = 6; return z; } } What is the result of attempting to compile and run the above program? A. true true B. true false C. false false D. An Exception is thrown at runtime public class Ouch { static int ouch = 7; public static void main(String[] args) { new Ouch().go(ouch); System.out.print(" " + ouch); } void go(int ouch) { ouch++; for(int ouch = 3; ouch < 6; ouch++); System.out.print(" " + ouch); } } What is the result of attempting to compile and run the above program? A. 5 8 B. 8 8 C. Compilation fails D. An Exception is thrown at runtime class Box { int size; Box(int s) { size = s; } } public class Laser { public static void main(String[] args) { Box b1 = new Box(5); Box[] ba = go(b1, new Box(6)); ba[0] = b1; for(Box b : ba)

2.

3.

4.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

Assignment 3
___________________________________________________________________________
System.out.print(b.size + " "); } static Box[] go(Box b1, Box b2) { b1.size = 4; Box[] ma = {b2, b1}; return ma; } } What is the result of attempting to compile and run the above program? A. 4 4 B. 5 4 C. 4 5 D. Compilation fails 5. class GFC402 { static int x=1; void m1(int i) { x++; i++; } public static void main (String[] args) { int y=3; m1(y); System.out.println(x + "," + y); } } What is the result of attempting to compile and run the above program? A. Prints: 2,3 B. Prints: 1,4 C. Prints: 2,4 D. Compile-time error class C { int i; public static void main (String[] args) { int i; //1 private int a = 1; //2 protected int b = 1; //3 public int c = 1; //4 System.out.println(a+b+c); //5 } } What is the result of attempting to compile and run the above program? A. Compile-time error at lines 1,2,3,4,5 B. Compile-time error at lines 2,3,4 C. Prints: 3 D. None of the above class GFC301 { private String name; public GFC301(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public static void m1(GFC301 r1, GFC301 r2) { r1.setName("Bird"); r2 = r1; } public static void main (String[] args) { GFC301 pet1 = new GFC301("Dog"); GFC301 pet2 = new GFC301("Cat"); m1(pet1,pet2); System.out.println(pet1.getName()+","+pet2.getName()); } } What is the result of attempting to compile and run the above program? A. Prints: Dog,Cat B. Prints: Dog,Bird C. Prints: Bird,Cat D. Prints: Bird,Bird

6.

7.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

Assignment 3
___________________________________________________________________________
8. class GFC304 { static void m1(int[] i1, int[] i2) { int[] i3 = i1; i1 = i2; i2 = i3; } public static void main (String[] args) { int[] i1 = {1}, i2 = {3}; m1(i1, i2); System.out.print(i1[0] + "," + i2[0]); } } What is the result of attempting to compile and run the above program? A. Prints: 1,1 B. Prints: 1,3 C. Prints: 3,1 D. Compile-time error class A { private static int counter; public static int getCounter() { return counter++; } private static int innerCounter; public static int getInnerCounter(){ return innerCounter++; } private String name; A() { name = "A" + getCounter(); } class B { private String name; B() { name = "B" + getInnerCounter(); System.out.print(A.this.name + name); // 1 } } public static void main(String[] args) { new A().new B(); // 2 A a1 = new A(); a1.new B(); // 3 a1.new B(); // 4 } } What is the result of attempting to compile and run the above program? A. Prints: A0B0A1B1A1B2 B. Prints: A0B0A1B1A2B2 C. Compile-time error at line 2 D. Compile-time error at line 4 class SRC111 { static String m(byte i) { return "byte"; } static String m(short i) { return "short"; } static String m(char i) { return "char"; } static String m(int i) { return "int"; } static String m(double i) { return "double"; } public static void main (String[] args) { byte b = 0; short s = 0; char c = 0; System.out.print(m(Math.min(b,b))+","); System.out.print(m(Math.min(s,s))+","); System.out.print(m(Math.min(c,c))); } } What is the result of attempting to compile and run the above program? A. Prints: int,int,int B. Prints: byte,short,int C. Prints: byte,short,char D. Compile-time error

9.

10.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

Assignment 3
___________________________________________________________________________
11. class MyString { String str; public String toString() { return str; } } class Test { public static void main(String arg[]) { String s1=new String(); MyString s2=new MyString(); System.out.println(s1+"$"+s2); } } What is the result of attempting to compile and run the above program? A. $null B. null$null C. $ D. Runtime error class Number { int num; void set(int n) { num=n; } } class Test { public static void main(String arg[]) { Number n1=new Number(); Number n2=new Number(); n1.set(5); n2.set(10); System.out.println(n1.equals(n2)); n1.set(15); n2.set(15); System.out.println(n1.equals(n2)); } } What is the result of attempting to compile and run the above program? A. truetrue B. truefalse C. falsetrue D. falsefalse class MyString { String str; MyString(String s) { this.str=s; } //Constructor MyString(MyString so) { this.str="copy string"; } //Copy Constructor public String toString() { return str; } } class Test { public static void main(String arg[]) { MyString s1=new MyString("hello"); MyString s2=s1; System.out.println(s2); } } What is the result of attempting to compile and run the above program? A. hello B. copy string C. null D. Compile-time error class Student { String name; int age; Student() { } Student(String name,int age) { this.name=name; this.age=age; }

12.

13.

14.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

Assignment 3
___________________________________________________________________________
String studentName() { return "Name: "+name.toUpperCase(); } String studentAge() { return "Age: "+age; } public String toString() { return studentName()+" "+studentAge(); } } class Test { public static void main(String arg[]) { Student s1=new Student(); Student s2=new Student("Manali",19); System.out.print(s1+" "+s2); } } What is the result of attempting to compile and run the above program? A. Name: MANALI Age: 19 B. Name: null Age: 0 Name: MANALI Age: 19 C. Name: NULL Age: 0 Name: MANALI Age: 19 D. Runtime error 15. class MyString { String str; public String toString() { return str; } } class Test { public static void main(String arg[]) { String s1=new String("null"); MyString s2=new MyString(); System.out.println(s1+"&"+s2+"&"+(s1.equals(s2))); } } What is the result of attempting to compile and run the above program? A. null&null&false B. null&&false C. null&&true D. Compile-time error class c1 { public void m1() { System.out.println("m1 method in C1 class"); } } class c2 { public c1 m1() { return new c1() { public void m1() { System.out.println("m1 method in anonymous class"); } }; } public static void main(String a[]) { c1 ob1 =new c2().m1(); ob1.m1(); } } What is the result of attempting to compile and run the above program? A. Prints: m1 method in C1 class B. Prints: m1 method in anonymous class C. compile time error D. Runtime error class Test { void printName(String name) { System.out.print("Name: "+name); } static void printAge(int age) { System.out.print("Age: "+age); } public static void main(String arg[]) { String name="Rishabh"; int age=22; Test t=new Test().printName(name); t.printAge(age); } } What is the result of attempting to compile and run the above program? A. Name: RishabhAge: 22 B. Name: Rishabh Age: 22 C. Compile-time error D. NullPointerException

16.

17.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

Assignment 3
___________________________________________________________________________
18. class Number { int num; Number(int n) { num=n; } Number(Number n) { num=n.num; } public String toString() { return num+""; } } class Test { public static void main(String arg[]) { Number n1=new Number(10); Number n2=n1; Number n3=new Number(n2); System.out.println((n1==n2)+" "+(n1==n3)+" "+(n2==n3)); } } What is the result of attempting to compile and run the above program? A. true false false B. true true false C. false false false D. false false true class Number { int num; Number(int n) { num=n; } int fun() { return num=num+50; } public String toString() { return num+" "; } } class Test { public static void main(String arg[]) { Number n[]=new Number[5]; for(int i=0;i<n.length;i++) n[i]=new Number(i*10); n[0]=n[n.length-1].fun(); for(int i=0;i<n.length;i++) System.out.print(n[i]); } } What is the result of attempting to compile and run the above program? A. 100 20 30 40 100 B. 90 10 20 30 90 C. Compile-time error D. Runtime error class Number { int num; void set(int n) { num=n; } public String toString() { return num+""; } } class Test { public static void main(String arg[]) { Number n[]=new Number[5]; for(int i=0;i<n.length;i++) (n[i]=new Number()).set(++i); for(int i=0;i<n.length;i++) System.out.print(n[i]+" "); } } What is the result of attempting to compile and run the above program? A. 1 null 3 null 5 B. 1 0 3 0 5 C. Compile-time error D. 1 followed by NullPointerException

19.

20.

___________________________________________________________________________
Jude Miranda www.neebal.com Phone : 022-26715310/12/13 Mobile : 09821308516

S-ar putea să vă placă și