Java Question P 4

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Question P 4 as PDF for free.

More details

  • Words: 3,385
  • Pages: 28
Java Certification Model Question & Answer - 4 -------------------------------------------------------------------------------Java Certification, Programming, JavaBean and Object Oriented Reference Books Sun Certified Programmer for the Java2 Platform Mock Exam QUESTIONS:Which of the following are valid assignment expressions, given the following declarations:byte b; int i = 10; long l = 20; b = i; b += i; b++; b = b + i; Answer -------------------------------------------------------------------------------State true or false: Interface methods can be declared static true false Answer -------------------------------------------------------------------------------Select the correct answers: If you define a class within an interface, the class is always public the class is always static the class methods cannot call methods declared in the interface Answer -------------------------------------------------------------------------------State true or false: An abstract class can have a constructor true false Answer -------------------------------------------------------------------------------What is the output of the following piece of code: class A { public A() { class B{

Gianeshwar Singh

1

B() { System.out.println("In no-arg constructor"); } } new B(); } public A(int a) { class B{ B() { System.out.println("In constructor with an int argument"); } } new B(); } public static void main( String[] args{ A a1 = new A(); A a2 = new A(10); } } Code does not compile Code compiles but generates exception at runtime Code compiles successfully and outputs In no-arg constructor In constructor with an int argument Answer -------------------------------------------------------------------------------Select all correct answers: Numeric operators that throw the ArithmeticException on integer operands include + / % * Answer -------------------------------------------------------------------------------What is the output of the following piece of code. System.out.println("-0.0 == 0.0 returns " + (-0.0 == 0.0)); System.out.println("0.0 > -0.0 returns " + (0.0 > -0.0)); ___________________________________

Gianeshwar Singh

2

Answer

-------------------------------------------------------------------------------Select true or false: The equality operator (==) always returns false if either operand is Nan and the inequality operator (!=) return true if either operand is Nan. true false Answer -------------------------------------------------------------------------------Select the correct answers: You can use an instance of a File class to do the following delete a file change current working directory rename files create new sub-directories Answer -------------------------------------------------------------------------------What is the output of the following code: public class Temp{ public static void main(String[] args){ String[] a = null; System.out.println("The length of the array is " + a.length): } } The code does not compile The code compiles and prints out "The length of the array is 0"; The code compiles and prints out "The length of the array is null"; The code compiles but generates the NullPointerException at runtime Answer -------------------------------------------------------------------------------Given the following declaration: char a; Which of the following are valid values that can be assigned to a? ‘\0’ ‘\u003c’

Gianeshwar Singh

3

‘\349’ \u004 ‘\345’ Answer -------------------------------------------------------------------------------Given the following code, what is the output generated class Base { public static void amethod(){ System.out.println("In base amethod"); } public void another(){ System.out.println("In base another"); } static int staticInt = 10; int instanceVar = 20; } public class Child extends Base { public static void amethod(){ System.out.println("In child amethod"); } public void another() { System.out.println("In child another"); } static int staticInt = 30; int instanceVar = 40; public static void main(String[] a ){ Base b = new Child(); b.amethod(); b.another(); System.out.println("Value of staticInt is = " + b.staticInt); System.out.println("Value of instanceVar is = " + b.instanceVar); } } Code does not compile Code compiles and prints out

In base amethod In child another Value of staticInt is 30

Gianeshwar Singh

4

Value of instanceVar is 40 Code compiles and prints out

In base amethod In child another Value of staticInt is 10 Value of instanceVar is 20 Code compiles and prints out

In child amethod In child another Value of staticInt is 30 Value of instanceVar is 40 Answer -------------------------------------------------------------------------------Select all correct answers: Modifiers that you CANNOT use with constructors include private abstract static public final Answer -------------------------------------------------------------------------------State true or false: The RandomAccessFile class is compatible with the Stream classes True False Answer

Gianeshwar Singh

5

-------------------------------------------------------------------------------What is the output of the following piece of code: class A{ public static void main(String[] args){ int i = 1; int j = 5; System.out.println((i++ * j)); System.out.println("i = " + i + " j = "+ j); System.out.println((++i * j)); System.out.println("i = " + i + " j = " + j); } } Code does not compile Code compiles and prints out 10 I = 2 j=5 15 I=3j=5 Code compiles and prints out 5 I = 2 j=5 15 I=3j=5 Answer -------------------------------------------------------------------------------ANSWERS:b, c

Remember, that a compound assignment expression of the form E1 op= E2 is always equivalent to E1 = (Type) (E1 op E2), where Type is the type of E1. b

Gianeshwar Singh

6

The only modifiers that are allowed with interface methods are public and abstract. And these are implicit, so you don’t even have to include them. a,b,c

All three are right. Classes defined within an interface are implicitly public and static and because you cannot have static methods within an iterface, you cannot refer to the non-static methods from the static class methods. a c c, d

The only operators that can cause an ArithmeticException are the integer division (/) and modulo (%) operators. Remember that float operations do not raise any exception at all. They may result in NaN or Infinities, instead. -0.0 == 0.0 returns true

-0.0 > 0.0 returns false a

Gianeshwar Singh

7

a, c, d d a, b, d

Check out the Java Language Specification for more information on Character Literal and Escape Sequences c

Static variables and methods as well as instance variables use the Type of the reference variable to determine the correct variable/method to use. On the other hand, instance methods use the Class of the reference variable to determine the correct method to call b,c,e

Unlike methods, a constructor cannot be abstract, static, final, strictfp, native or synchronized. b c Back to questions Return to : Java Programming Hints and Tips -------------------------------------------------------------------------------Java Certification, Programming, JavaBean and Object Oriented Reference Books Sun Certification for Java2 Programming QUESTION what is the output of following code?

Gianeshwar Singh

8

public class Test { public static void main(String[] args){ int i = 4, j = 7; String k = "string"; System.out.println(i+j+k+i+j); } } OPTION a. 47sting47 b. 11string11 c. 47string11 d. 11string47 e. Non of above is correct ANSWER d

-------------------------------------------------------------------------------QUESTION What will happen if compile and run the following code? public class Test { public static void main(String[] args){ StringBuffer k = new StringBuffer("string"); StringBuffer s = new StringBuffer("String"); if (s.equals(k)) System.out.println("Yes"); else System.out.println("No"); } } OPTION a print out "Yes" b print out "No" c compile error d run time exception e. Non of above is correct ANSWER b

Gianeshwar Singh

9

-------------------------------------------------------------------------------QUESTION You design a GUI with lots of components whose fonts and background colours are different, it looks very nice. Because Java is platform independent, its appearance will be excectly the same in other platform. OPTION a. Yes b. Yes, but only if use same Java version c. No d. No, because Java is not platform independent. ANSWER c

-------------------------------------------------------------------------------QUESTION what is the result of the following code long i = 1,j; j = i<< 35; System.out.println(j) OPTION a. copmpiling or run time error b. 2 power 3 = 8 c. 2 power 5 = 32 d. 2 power 34 e. 2 power 35 f. 2 power 36 ANSWER e

-------------------------------------------------------------------------------QUESTION what will appear on screen after perform following code

Gianeshwar Singh

10

public class Test{ public static void main(String[] args){ From f = new Frame("Test"); Button a = new Button("A"); Button b = new Button("B"); Button c = new Button("C"); Button d = new Button("D"); f.add(a); f.add(b); f.add(c); f.add(d); } } OPTION a. A button label "D" in centre of a frame, occupying whole frame. b. Four buttons label "A","B","C" and "D" lay one by one inside the frame. c. there are four buttons on screen d. see nothing on screen ANSWER d

-------------------------------------------------------------------------------QUESTION what is default layout of dialog component

OPTION a. FlowLayout b. BorderLayout c. GridLayout d. CardLayout e. GridBagLayout ANSWER b EXPLANATION default layout for applet is FlowLayout Frame has BorderLayout same as Dialog

Gianeshwar Singh

11

-------------------------------------------------------------------------------QUESTION int j; for (int i =3,j=1;j++,i--;j<3) if (j== i) contiune; System.out.println("i="+i+" j="+j) what is the output of above code fragment.

OPTION a. run time error or exception throw b. compiling error c. i = 1 j = 1 d i=2j=1 e. i = 3 j = 1 f. i = 2 j = 2 g. i = 2 j = 3 h. i = 3 j = 3 ANSWER b EXPLANATION for statment initial error, cannot mix initial and assigment together.

-------------------------------------------------------------------------------QUESTION class Outer{ static class Inner{ } } Given that in your class myApp, you need a reference to Inner object. which of following code fragment shows the correct way to declare and initialize a reference to a Inner object OPTION a. Outer.Inner myInner = new Outer.Inner()

Gianeshwar Singh

12

b. Inner myInner = new Outer.Inner() c. Outer.Inner myInner = Outer.new Inner() d. Inner myInner = Outer.new Inner() f. Outer.Inner myInner = new Outer.new Inner() e. Inner myInner = new Outer.new Inner() g. Inner myInner = new Outer.Inner()

ANSWER g

-------------------------------------------------------------------------------QUESTION class Outer{ class Inner{ } } Given that in your class myApp, you need a reference to Inner object. which of following code fragment shows the correct way to declare and initialize a reference to a Inner object OPTION a. Outer.Inner myInner = new Outer.Inner() b. Inner myInner = new Outer.Inner() c. Outer.Inner myInner = new Outer.new Inner() d. Inner myInner = new Outer.new Inner() e. Outer outer = new Outer(); Outer.Inner myInner = new Outer.new Inner()

ANSWER c e

-------------------------------------------------------------------------------QUESTION //file Test.java class A implements Runnable{ public void run(){ i+=1;

Gianeshwar Singh

13

} void printi(){System.out.println("i="+i);} int i = 1; } public class Test{ public static void main(String[] args){ A a= new A(); a.printi(); Thread t = new(a); t.start(); a.i +=1; a.print(); } } compile and run above code, what will be output? OPTION a. compile error b. run time error c. i = 1, i = 1; d. i = 1, i = 2; h. output unpredictable ANSWER h EXPLANATION two thread shared the same data, should use synchronized key word

-------------------------------------------------------------------------------QUESTION class Parent{ int i = 0; void amethod(){System.out.println("in Parent")}; } class Child extends Parent{ int i = 10; void amethod(){System.out.println("in Child")}; }

Gianeshwar Singh

14

class Test{ public static void main(String[] args){ Parent p = new Child(); Child c = new Child(); System.out.print("i="+p.i+" "); p.amethod(); System.out.print("i="+c.i+" "); c.amethod(); } } what will appear in output of above code? OPTION a. i = 0 in Parent b. i = 0 in Child c. i = 10 in Parent d. i = 10 in Child c. compiler or run time error ANSWER b d EXPLANATION only member method can be overriden, not member variable

-------------------------------------------------------------------------------QUESTION class A{ int j; A(int i){j = i;} } public class B extends A{ B(int i){j=i*2;} public static void main(String[] arges){ A b = new B(1); System.out.println("b.j = "+b.j); } }

Gianeshwar Singh

15

what is output of above code? OPTION a. compile error b. run time error c. b.j = 1 d. b.j = 2 ANSWER a EXPLANATION Deault constraction must be defined, if subclass constraction need to use it.

-------------------------------------------------------------------------------QUESTION class Test{ public static void main(String[] arg){ short i,j; i = 10; j= -i*10; System.out.println("j="+j); } } what will be output after compile and run about code. OPTION a. compile error b. runtime error c. j= -100 d. non of above is correct ANSWER a EXPLANATION in j = -i*10// "-" turn -i to be int

--------------------------------------------------------------------------------

Gianeshwar Singh

16

QUESTION which of following statements are true. select all correct answer. a). A private number(method or variables) only can be accessed under current instance. b). A class number(class,method or variable) may have one of following access modifier; public, default, protected or private. c). The only access modifier you can put before a top level class is public. d). Assume class C extends B, class B extends A, class A has a method action(), which is overridden by class B and class C. But inside an instance of class C, you still can get action() of class A version,by use "super.super.action()". e). Protected feature means it available to all classes in the same package, and available to all subclasses of the class OPTION a is correct b is correct c is correct d is correct e is correct d none of them correct ANSWER c e EXPLANATION a, private means private to class, i.e can accessed by any instance of that class b, default is not a legal access modifier. d, "super.super." is illegal.

-------------------------------------------------------------------------------QUESTION variables a,b,c and results are type of long. what is the best way to caculating results = a*b/c ? OPTION a). results = a*(b/c) b). results = (a*b)/c c). results = a/c*b

Gianeshwar Singh

17

d). a,b,c will produce exactly the same results. ANSWER b EXPLANATION for integer 9/2 equal to 4

-------------------------------------------------------------------------------QUESTION Assume a and result are float, i.e float a, result; after caculating as; result = sqt(a); How can you know that variable results is a NaN OPTION a). if (result >= Float.NaN) b). if (result <= Float.NaN) c). if (result == Flaot.NaN) d). use try{} and catch to catch exception e). non of a,b,c and d are correct. ANSWER e EXPLANATION the correct way to compare NaN is use Float.isNaN(float i)

-------------------------------------------------------------------------------QUESTION which is legal? OPTION a. boolean isboolean = null; b. char c = '\u4507'; c. double d = 1.0/0.0; d. byte a = 256; e. String str = null; f. String str = "c:\";

Gianeshwar Singh

18

ANSWER b c e EXPLANATION a, boolean type only can take false or true as value c, d= +infinity d, out of range f, correct is String str = "c:\\"

-------------------------------------------------------------------------------QUESTION True or False; Assign null to a reference variable which you have finished with it can improve performence. OPTION a. true b. false ANSWER a

-------------------------------------------------------------------------------QUESTION which is true? OPTION a). Shift operators may be applied only to oprands of either int or long b). A array declare inside a mothed will not be initialized by system just like methed local variable. c). JVM need Garbage Collection because reference variables allocated on the stack while object is allocated on the heap. d). Java has Garbage Collection ability, so software written by Java will never have memory leaks or run out of memory problem ANSWER a c

Gianeshwar Singh

19

EXPLANATION b false array always get initialzed by system d false if you keep create object, sooner or later menory will run out.

-------------------------------------------------------------------------------QUESTION class Test{ public static void main(String args[]){ int total; for(int i = 0 ; i<10;){ total += i; i++; } System.out.println("total = "+total+" : i = "+i); } } what is output of above code? OPTION a). compile error b). run time error c). output: total = 55 : i = 10 d). output: total = 55 : i = 9 ANSWER a EXPLANATION System.out.println("total = "+total+" : i = "+i), i out of scope

-------------------------------------------------------------------------------QUESTION which of the following code is legal? OPTION a). int total = 0; for (int i=7, long j=0;i<10;j++) total += i;

Gianeshwar Singh

20

b). int x =038 System.out.println("x="+x); c). public class A{ public amethod(int i){ public int j=0; j=i*2; } } d). int x =034 System.out.println("x="+x); ANSWER d

-------------------------------------------------------------------------------QUESTION which of the following construction is legal OPTION a). RandomAccessFile("Text.txt", "rw") b). RandomAccessFile("Text.txt", 'r') c). RandomAccessFile("Text.txt", "r") d). RandomAccessFile("Text.txt", "w") e). RandomAccessFile("Text.txt", 'w') ANSWER a c

-------------------------------------------------------------------------------QUESTION which collections can have duplicates data OPTION a). Collection b). List c). Set

Gianeshwar Singh

21

d). Map ANSWER a b

-------------------------------------------------------------------------------QUESTION Witch of the following statement is true. OPTION a). An object whose class implement Runnable interface, if you call run() in main thread instead of start(), run() will not execute. b). The signature of main is, static public main(String[] arge) c). Assume a is a Array, statement a.length = 10, will get compiler error. ANSWER c

-------------------------------------------------------------------------------QUESTION Witch of the following is legal. OPTION a). float a = 1; b). float a = 1.0; c). char a = \u0000; d). char a = 12; e). int i = 12; char a = i; ANSWER a d EXPLANATION b. should be; float a = 1.0f; c. should be; char a = '\u0000' e. should be; char a = (char)i;

Gianeshwar Singh

22

-------------------------------------------------------------------------------QUESTION What is '\u0000' represented? OPTION a). null character b). equivalent to decimal 0(zero) c). space ascii d). a,b,c and d all wrong ANSWER a b

-------------------------------------------------------------------------------QUESTION What is the ouput of the following code? public class test { static StringBuffer str = new StringBuffer("Hello world"); public static void amethod1(StringBuffer s){ s = new StringBuffer("Good bye world"); } public static void amethod2(StringBuffer s){ s.append(" !"); } public static void main(String[] args){ amethod1(str); System.out.println(str); amethod2(str); System.out.println(str); } } OPTION a). Hello world Hello world ! b). Hello world Hello world c). Good by world Good by world d). Good by world Good by world ! e). Good by world Hello world d). Hello world Good by world

Gianeshwar Singh

23

f). Good by world Hello world ! g). Hello world Good by world ! ANSWER a

-------------------------------------------------------------------------------QUESTION Assume str = "red", compile and run a program which will call the following code, the results will be, boolean amethod(String str){ switch (str){ case "Red" case "red" System.out.println("It is Red!"); case "Blue" case "blue": System.out.println("It is Blue!"); break; case "Green": case "green": System.out.println("It is Green!"); } } OPTION a). output "It is Red!" b). output "It is Blue!" c). output "It is Green!" d). output "It is Red! ", follow by "It is Blue!" e). run time error. f). compile time error. ANSWER f

-------------------------------------------------------------------------------QUESTION Select the best statment to replace the comment of line 1.

Gianeshwar Singh

24

try{ // line 1 BufferedReader b = new BufferedReader(f); } catch(FileNotFoundException e){ System.out.println("FileNotFoundException "+e); } OPTION a). BufferedInputStream f = new BufferedInputStream("text.txt"); b). File f = new File("text.txt"); c). FileInputStream f = new FileInputStream("text.txt"); d). FileReader f = new FileReader("text.txt"); ANSWER d

-------------------------------------------------------------------------------QUESTION Which of the following statments is true? OPTION a).Java arrays may be assume as static arrays, that means size has to be specified at compile time. b).Thread is a abstract class, as method void run() must be implmented. c). With &,^ and | operations, the two operands may be of any types of boolean, byte, short, int and long, even mix of these types, just like +, - operatins. d). With operations && and ||, they only can be apply to boolean operands. ANSWER a

-------------------------------------------------------------------------------QUESTION Compile and run the following code, what will happen? class Try{ public static void main(String arg[]){ int i,j,k; k = 2; for( i=0; i < k*2; j=(i++)*2) System.out.println("j="+j);

Gianeshwar Singh

25

} } OPTION a). j=2 will appear in output b). run time error c). compiler error ANSWER c EXPLANATION compiler gives error message as " variable j might not have been initialized"

-------------------------------------------------------------------------------QUESTION Select the best statement that can set int i to Label lab, so that lab can display variable i. Assume; Label lab = new Label(); int i = 100; OPTION a). lab.setText(i); b). lab.setText(i.toString()); c). lab.setText(Integer.toString(i)); d). Integer ii = Integer(i); lab.setText(ii.toString()); ANSWER c

-------------------------------------------------------------------------------QUESTION What will be the output of the following code? //file Test.java class Parent{ int i = 0; void amethod(){System.out.println("in Parent");}; }

Gianeshwar Singh

26

class Child extends Parent{ int i = 10; void amethod(){System.out.println("in Child");}; } public class Test{ public static void main(String[] args){ Parent p = new Child(); Child c; System.out.print("i="+p.i+" "); p.amethod(); c = (Child)p; System.out.print("i="+c.i+" "); c.amethod(); } } OPTION a). i=10 in Child follow by i=0 in Child b). i=10 in Child follow by i=0 in Child c). i=0 in Child follow by i=10 in Child d). i=0 in Child follow by i=0 in Child e). Non of above is correct ANSWER c

-------------------------------------------------------------------------------QUESTION what will be the output of the following code. //File Test.java class Parent{ private void amethod(){System.out.println("in Parent");}; } class Child extends Parent{ void amethod(){System.out.println("in Child");}; } public class Test{ public static void main(String[] args){ Parent p = new Child(); p.amethod();

Gianeshwar Singh

27

} } OPTION a). in Parent will appear in output b). in Child will appear in output c). run time error d). compile error ANSWER d EXPLANATION compile error message as; amethod() has private access in Parent Return to : Java Programming Hints and Tips

--------------------------------------------------------------------------------

Gianeshwar Singh

28

Related Documents

Java Question P 4
November 2019 10
Java Question
November 2019 14
Java Java Notes 4
November 2019 66
Java Question Paper
November 2019 24
Question 4
November 2019 8
Question 4
July 2020 10