Exam 5 Questions

  • October 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 Exam 5 Questions as PDF for free.

More details

  • Words: 2,071
  • Pages: 11
Dan Chisholm

Exam 5

Questions

Question 1 class GFC403 { private static int x=1; static 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 program? a. b. c. d. e. f. g.

Prints: 1,3 Prints: 2,3 Prints: 1,4 Prints: 2,4 Run-time error Compile-time error None of the above

Question 2 A class can not be called "tightly encapsulated" unless which of the following is true? a. b. c. d. e.

The class is declared final. All local variables are declared private. All method parameters are declared final. No method returns a reference to any object that is referenced by an internal data member. None of the above

Question 3 Which of the following methods are deprecated members of the Object or Thread class? a. b. c. d. e. f. g. h. i. j. k. l.

countStackFrames join notify notifyAll resume run sleep start stop suspend yield wait

Question 4 class C { public static void main (String[] args) { System.out.print(Math.round(Float.NaN)); }}

Page: 1

Dan Chisholm

Exam 5

Questions

What is the result of attempting to compile and run the program? a. b. c. d. e. f.

Prints: NaN Prints: 0.0 Prints: 0 Compile-time error Run-time error None of the above

Question 5 class D { public static void main (String args[]) { Byte a = new Byte("1"); byte b = a.byteValue(); short c = a.shortValue(); char d = a.charValue(); int e = a.intValue(); long f = a.longValue(); float g = a.floatValue(); double h = a.doubleValue(); System.out.print(b+c+d+e+f+g+h); }} What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: 7 Prints: 7.0 Compile-time error Run-time error None of the above

Question 6 class A { public static void main (String args[]) { Integer i1 = new Integer(1); Integer i2 = new Integer(i1); System.out.print(i1.equals(i2)); }} What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: false Prints: true Compile-time error Run-time error None of the above

Question 7 class C { public static void main (String args[]) { Long a = new Long(1); byte b = a.byteValue(); short s = a.shortValue();

Page: 2

Dan Chisholm

Exam 5

Questions

char c = a.charValue(); int d = a.intValue(); long e = a.longValue(); float f = a.floatValue(); double g = a.doubleValue(); System.out.print(b+s+c+d+e+f+g); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f.

Prints: 7 Prints: 7L Prints: 7.0 Compile-time error Run-time error None of the above

Question 8 class B { public static void main (String args[]) { Double a = new Double(0xFFFF); byte b = a.byteValue(); short c = a.shortValue(); int e = a.intValue(); long f = a.longValue(); float g = a.floatValue(); double h = a.doubleValue(); System.out.print(b+","+c+","+ (e+f+g+h == 4 * 0xFFFF)); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.

Prints: 0xFFFF,0xFFFF,false Prints: 0xFFFF,0xFFFF,true Prints: -1,-1,false Prints: -1,-1,true Compile-time error Run-time error None of the above

Question 9 class C { public static void main(String[] args) { Boolean b1 = Boolean.valueOf(true); Boolean b2 = Boolean.valueOf(true); Boolean b3 = Boolean.valueOf("TrUe"); Boolean b4 = Boolean.valueOf("tRuE"); System.out.print((b1==b2) + ","); System.out.print((b1.booleanValue()==b2.booleanValue()) + ","); System.out.println(b3.equals(b4)); }} What is the result of attempting to compile and run the program? a. Prints: false,false,false

Page: 3

Dan Chisholm b. c. d. e. f. g. h. i. j. k.

Exam 5

Questions

Prints: false,false,true Prints: false,true,false Prints: false,true,true Prints: true,false,false Prints: true,false,true Prints: true,true,false Prints: true,true,true Compile-time error Run-time error None of the above

Question 10 Which of the instance creation expressions produce a run-time error? a. b. c. d. e. f. g.

new Float('A') new Float("A") new Float(1L) new Float("1L") new Float(0x10) new Float("0x10") new Float("010")

Question 11 Which of the following class instance creation expressions would generate a run-time error? a. b. c. d. e. f.

new Short("1") new Short("-1") new Short("+1") new Short("1.0") new Short("0x1") new Short("011")

Question 12 • •

Entries are organized as key/value pairs. Duplicate entries replace old entries.

Which interface of the java.util package offers the specified behavior? a. b. c. d.

List Map Set None of the above

Question 13 import java.util.*; class D { public static void main (String args[]) { AbstractSet a = new HashSet(); System.out.print((a instanceof Set)+",");

Page: 4

Dan Chisholm

Exam 5

Questions

System.out.print(a instanceof SortedSet); }} What is the result of attempting to compile and run the program? a. b. c. d. e.

Prints: false,false Prints: false,true Prints: true,false Prints: true,true None of the above

Question 14 If two instances of a class type are not equal according to the equals method, then the same integer value must not be returned by the hashCode method of the two objects. a. false b. true Question 15 class MWC104 { public static int[5] a1; int []a2; int[ ]a3; int a4[]; }}

void main(String[] args) { // 1 // 2 // 3 // 4

A compile-time error is generated at which line? a. b. c. d. e.

1 2 3 4 None of the above

Question 16 Suppose that the compiler generates a default constructor for a class. If a compile-time error is to be avoided, which of the following must be true? a. b. c. d. e.

The superclass must not have any constructor other than a default constructor. The superclass must not have an accessible no-argument constructor. The no-argument superclass constructor must not have a throws clause that includes a checked exception. The no-argument superclass constructor be declared private. None of the above

Question 17 Which of the following statements are true? a. A value can not be assigned to a final field more than once. b. A value can be assigned to a final field at any time or not at all.

Page: 5

Dan Chisholm

Exam 5

Questions

c. Only static variables can be declared final. d. A compile-time error is thrown if a blank final instance variable is not assigned a value before the end of each constructor. e. A field can not be declared both final and volatile. Question 18 Which of the following statements is true? a. b. c. d. e.

An abstract method can not be overridden by an abstract method. An instance method that is not abstract can not be overridden by an abstract method. An abstract method declaration can not include a throws clause. The body of an abstract method is represented by a set of empty brackets. None of the above.

Question 19 class Maroon { public static void int a = 1; // short b = 1; // long c = 1; // a = c + a; // c = b + a; // }}

main (String[] args) { 1 2 3 4 5

A compile-time error is generated at which line? a. b. c. d. e. f.

1 2 3 4 5 None of the above

Question 20 interface I1 {} interface I2 {} class Base implements I1 {} class Sub extends Base implements I2 {} class Yellow { public static void main(String args[]) { Base base = new Sub(); // 1 I1 i1 = base; // 2 Sub sub = (Sub)base; // 3 I2 i2 = (Sub)base; // 4 }} A compile-time error is generated at which line? a. b. c. d.

1 2 3 4

Page: 6

Dan Chisholm

Exam 5

Questions

e. None of the above Question 21 import java.io.Serializable; class Blue { public static void main (String args[]) { int[] i = {1,2,3}; // 1 Serializable s = i; // 2 i = (int [])s; // 3 }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.

Compile-time error at line 1. Run-time error at line 1. Compile-time error at line 2. Run-time error at line 2. Compile-time error at line 3. Run-time error at line 3. None of the above

Question 22 class GFC217 { static String m(int i) {return "int";} static String m(float i) {return "float";} public static void main (String[] args) { long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1)); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.

Prints: float,float Prints: float,double Prints: double,float Prints: double,double Compile-time error Run-time error None of the above

Question 23

class E { void printS1(){System.out.print("E.printS1 ");} static void printS2() {System.out.print("E.printS2");} } class F extends E { void printS1(){System.out.print("F.printS1 ");} static void printS2() {System.out.print("F.printS2");} public static void main (String args[]) { E x = new F(); x.printS1(); x.printS2(); }}

Page: 7

Dan Chisholm

Exam 5

Questions

What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.

Prints: E.printS1 E.printS2 Prints: E.printS1 F.printS2 Prints: F.printS1 E.printS2 Prints: F.printS1 F.printS2 Run-time error Compile-time error None of the above

Question 24 class C { private static String s1 = "s1"; String s2 = "s2"; C() {m1("s5","s6");} void m1(final String s5, String s6) { final String s3 = "s3"; String s4 = "s4"; class Z {Z() {System.out.print(???);}} new Z(); } public static void main(String args[]) {new C();} } Which variable names can be substituted for ??? without causing a compile-time error? a. b. c. d. e. f.

s1 s2 s3 s4 s5 s6

Question 25 class A {void m1(A a) {System.out.print("A");}} class B extends A {void m1(B b) {System.out.print("B");}} class C extends B {void m1(C c) {System.out.print("C");}} class D { public static void main(String[] args) { A c1 = new C(); B c2 = new C(); C c3 = new C(); C c4 = new C(); c4.m1(c1); c4.m1(c2); c4.m1(c3); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f.

Prints: AAA Prints: ABC Prints: CCC Compile-time error Run-time error None of the above

Question 26

Page: 8

Dan Chisholm

Exam 5

Questions

class Yellow { public static void main(String args[]) { String a = "A"; String b = "B"; String c = a+b; String d = a+b; System.out.print((c==c) + ","); System.out.print(((a+b)==(a+b)) + ","); System.out.print(c==d); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g. h. i. j. k.

Prints: false,false,false Prints: false,false,true Prints: false,true,false Prints: false,true,true Prints: true,false,false Prints: true,false,true Prints: true,true,false Prints: true,true,true Compile-time error Run-time error None of the above

Question 27 class H { public static void main (String[] args) { StringBuffer sb1 = new StringBuffer("ABC"); StringBuffer sb2 = new StringBuffer("ABC"); System.out.print((sb1==sb2)+","+sb1.equals(sb2)); }} What is the result of attempting to compile and run the program? a. b. c. d. e. f. g.

Prints: false,false Prints: false,true Prints: true,false Prints: true,true Compile-time error Run-time error None of the above

Question 28 class MWC204 { public static void main(String[] args) { int[][] a1 = {{1,2},{3,4,5},{6,7,8,9},{}}; System.out.print(a1.length); }} What is the result of attempting to compile and run the program? a. Prints: 0 b. Prints: 3

Page: 9

Dan Chisholm c. d. e. f. g. h. i.

Exam 5

Questions

Prints: 4 Prints: 9 Prints: 10 Prints: 11 Compile-time error Run-time error None of the above

Question 29 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 program? a. b. c. d. e. f. g.

Prints: 1,1 Prints: 1,3 Prints: 3,1 Prints: 3,3 Run-time error Compile-time error None of the above

Question 30

abstract class A { private int x = 4; private int y = 2; public int x() {return x;} public void x(int x) {this.x = x;} public int y() {return y;} public void y(int y) {this.y = y;} } interface B {int math();} class C { static class D extends A implements B { public int math() {return x()+y();} } static A a1 = new A() implements B { public int math() {return x()+y();} }; public static void main(String[] args) { System.out.print(new C.D().math()); System.out.print(a1.math()); }} What is the result of attempting to compile and run the program? a. Prints: 12 b. Prints: 66

Page: 10

Dan Chisholm

Exam 5

Questions

c. Compile-time error d. Run-time error e. None of the above

Page: 11

Related Documents

Exam 5 Questions
October 2019 6
Exam Questions
June 2020 12
Exam Questions
July 2020 7
Exam Questions
June 2020 14
Exam Questions
June 2020 14