CSE 2221 Final Exam Review

Ace your homework & exams now with Quizwiz!

The reference value of a reference variable is related to the objects mathematical value

F They are not correlated at all it is placed randomly throughout the memory.

Instance methods may be static or non-static T F

F They are only non-static which means they need to be defined before being used.

Instance methods can only be private T F

F They can also be public

in the call n.foo(x), x is known as the receiver of the call

F X is the actual parameter

Which of the following is true given the diagram to the right? Class 1 ^ | extends Class 2 A class c1 is a super class of C2 B class c1 is the implementer view, class c2 is the clientview C class c1 inherits all of the methods implemented in class C2. D class c1 is the client view, class c2 is the implementer view

A class c1 is a superclass of C2

Testing the behaviors of a single method or component is better known as A unit testing B integration testing C system testing D implementer testing

A unit testing

What can you say about these two methods, m1 and m2? Private static boolean m1(int x,int y) { boolean result; if (x>0&&y>0) { result=false; } else { result=true; } return result; } private static boolean m2(int x,int y) { return !(x>0&&y>0); } A) They are both syntactically correct Java and return the same results for all values of x and y. B) Method m1 contains syntax errors C) Method m2 contains syntax errors D) They are both syntactically correct Java return differ

A) They are both syntactically correct Java and return the same results for all values of x and y.

What parameter mode is the default? That is which parameter mode should be assumed if no other parameter mode is explicitly indicated in the contract? A) restores B) clears C) replaces D)updates

A) restores

Consider the following code fragment: int x=8; while((x/3)!=2) { x=x-1 } How many times will the body of the loop execute? A) 0 B) 1 C) 2 D) The loop will never terminate.

A)0

What is the value of k after this code completes? int i =0,j=0,k=0; while(i<3){ i++; j=0; while(j<3){ j++ k=k+i+j; } } A)36 B)54 C)9 D)6

A)36

Which of the statements about this diagram is true? I1 ^extends I2 ^implements C3 ^extends C4 A)C4 implements the methods described in I2 B)The arrows are labeled incorrectly C)i1 inherits all the methods declared in I2 D)C3 is a description of the behavior/functionality provided by the code in I2.

A)C4 implements the methods described in I2

Given the following code and assuming that all necessary imports and the rest of the class are provided correctly, which statement is true? SimpleWriter out=new SimpleWriter1L(); NaturalNumber n1=new NaturalNumber1L(5); NaturalNumber n2=n1; n2.decrement(); out.println("n1="+n1+", n2=" + n2); A)will output "n1=4, n2=4" B)will output "n1=5, n2=4" C)will not compile D)will result in a run-time error

A)will output "n1=4, n2=4"

Under design-by-contract which of the following statement is true? A the implementer may asssume that the post condition(ensures clause) is true when writing the method. B The implementer may assume that the precondition (requires clause) is true when writing the method. C The implementer must make sure that the precondition (requires clause) is true when the method returns D The implementer must write the method so it throws an exception if the client supplies input values that do not satisfy the requires clause.

B The implementer may assume that the precondition (requires clause) is true when writing the method.

A java compiler does the following: A) Checks a bytecode program in a ".class" file for run-time errors and if there are none, if generates source code for that program in a ".java" file. B) Checks a source code program in a ".java" file for run-time errors and if there are none, if generates bytecode for that program in a ".class" file. C) Checks a source code program in a ".java" file for complie-time errors and if there are none, if generates bytecode for that program in a ".class" file. D) Checks a bytecode program in a ".class" file for compile-time errors and if there are none, if generates source code for that program in a ".java" file. E) None of the above

B or C

Consider the following array declaration and initialization: String[] a={"A","B","C","D"}; What is the value of the expression (a[1]+a[3])? A)"AC" B)"BD" C)"ABC" D)"CD"

B)"BD"

The nextDouble() method of r, a variavle of type Random, returns a double in the range of [0.1). Which of thse expressions would you use to create a random range of [-3,3]: A) r.nextDouble ()*3.0-3.0 B)( r.nextDouble ()-.5)*6.0 C) r.nextDouble ()*6.0+3.0 D) r.nextDouble ()*6.0+0.5 E) (r.nextDouble ()-3.0)*6.0

B)( r.nextDouble ()-.5)*6.0

Which of the statements about this diagram is true? I1 ^extends I2 ^implements class C A)C is a description of what I2 does, not of how it does it B)C implements the behavior/functionality provided in i1. C)I1 inherits all the methods declared in I2 D)I2 is the implementer view and I1 is the client view

B)C implements the behavior/functionality provided in i1.

The Crucial Theorem for recursion is A) To curse is human, to recurse is divine B)If your code is correct when it calls a hypothetical freelunch version of the method on a smaller version of the problem it will still be correct with a recursive call to your own version C)All recursion solutions can be done iteratively but at a performance cost D)Recursive solutions are always more efficient than iterative solutions for the same problem.

B)If your code is correct when it calls a hypothetical freelunch version of the method on a smaller version of the problem it will still be correct with a recursive call to your own version

Which of the following is not true of Instance Methods? A) may be public or private B)May be called without a receiver C)May return any type or nothing(void) D)May be declared in an interface

B)May be called without a receiver

Which is not a property of an RSS 2.0 feed? A)The root is an rss node with a version attribute whose value is "2.0". B)There are one or more channel nodes as a child of the root. C)The channel node must have one of each of these nodes as children: title,link, and description. D)The channel node can also have zero or more item child nodes plus other optional children.

B)There are one or more channel nodes as a child of the root.

After the two variables are declared and initialized, which java instruction will set the value of b to TRUE? NaturalNumber n= new NaturalNumber2(); NaturalNumber k= new NaturalNumber2(1); A)boolean b=k.compareTo(n)==1; B)boolean b=n.compareTo(k)<0; C)boolean b=n<=k D)boolean b=n.!equals(k);

B)boolean b=n.compareTo(k)<0;

For this line of XML what is the attribute name? <parts dist="eve">5</parts? A)parts B)dist C)even D)5

B)dist

When testing a methods functional correctness what does i mean for a method to be correct? A)it compiles and runs without errors. B)it does what it is supposed to do, and it doesn't do what it's not supposed to do. C)it passes all of the test cases. D)It does not have any performance issues

B)it does what it is supposed to do, and it doesn't do what it's not supposed to do

The checkstyle plugin for Eclipse is useful because A)it warns you to potential compile-time errors B)it helps you make your code understandable for yourself and other programmers C)it prevents your code from making errors caught by asset statements D)it tells you when code you have written does not obey its contract

B)it helps you make your code understandable for yourself and other programmers

Indicating that a parameter is "replaces" mode A) Means the parameter's value might be change from its incoming value that depends on its incoming value B)means that the outgoing value in fact the entire behavior of the method is independent of the incoming value of that parameter C)is the equivalent of adding ... and x=#x to the ensures clause D)is the equivalent of adding ... and x=#x to the requires clause

B)means that the outgoing value in fact the entire behavior of the method is independent of the incoming value of that parameter

For a method formal parameter p, which parameter mode guarantees that #p cannot appear n the ensures clause for the method's contract? a. clears b. replaces c. restores d. updates

B. replaces

Consider the following method contract snippet @????? y @ensures y=z*2 public static void foo(NaturalNumber y,NaturalNumber z) Which of the following parameter modes is the correct one for the given y above? A clears B restores C replaces D updates

C replaces

Consider the following method: private static int m(int x,int y){ y=x; x=0; return y; } and the client code: int num 1=4, num2=9; num2=m(num1,num2); Which is true after the client code executes? A) num1=0, num2=4 B) num1=0, num2=9 C) num1=4, num2=4 D) num1=4, num2=9

C) num1=4, num2=4

NaturalNumber[] nums=new NaturalNumber[2]; nums[0]=new NaturalNumber1l(1); nums[1]=new NaturalNumber1l(2); NaturalNumber b=new NaturalNumber(4); b.add(b.divide(nums[1])); A)6 B)4 C)2 D)8

C)2 b=4 num[1]=2 b.divide(num[1])=0 b=2 b.add(0)=2

Which interface or class implementation of power will Java use for this call to power? NaturalNumber k=new NaturalNumber2(); NaturalNumber n=new NaturalNumber2Override(); NaturalNumber j=n; j.power(2); A)NaturalNumber B)NaturalNumber2 C)NaturalNumber2Override D)NaturalNumberKernel

C)NaturalNumber2Override

An XMLTree created from an RSS 2.0 feed will have which of these properties? A)The root is a node with the <channel> tag B)Child 0 of the <channel> tag node must be a <title> tag node. C)No particular order is required among the children of an <item> tag node. D)At least one child of the <item> tag node must be a <title> tag node

C)No particular order is required among the children of an <item> tag node.

What is true of an XMLTree? A)Only tag nodes have a label property B)Every node has an attribute property C)Only tag nodes may have children D)Only non-tag nodes can be leaf nodes

C)Only tag nodes may have children

The interval-halving technique can be used to efficiently compute results for problems like guessing numbers and finding roots because: A)In java division by two throws away the remainder. B)All numbers involved in the computation are non-negative. C)The answer to all questions you can ask to get information about the mid-point of the interval allows you to eliminate (as possible results) all points on one side of the mid-point. D) The function you are evaluating over the interval has no more than two maximum or minimum values in that interval

C)The answer to all questions you can ask to get information about the mid-point of the interval allows you to eliminate (as possible results) all points on one side of the mid-point.

Consider the following method signature: private static int examScore(int StudentNum) Here, StudentNum is called: A) distinguished variable B)an argument C)a formal parameter D)an Index

C)a formal parameter

The recursion "confidence building" argument A) STarts by proving that the method works for the largest value B) starts by proving that the method does not work for the largest value C)is most like mathematical induction D)is most like proof by contradiction

C)is most like mathematical induction

What is the value of num after the following loop? NaturalNumber n= new NaturalNumber2(25); NaturalNumber hund=new NaturalNumber2(100); NaturalNumber num=n; while(n.compareTo(hund)<0){ n.multiply(new NaturalNumber2(2)); if(n.compareTo(num)>0) { num=n; } } A)num=25 B)num=50 C)num=100 D)num=200

C)num=100

Consider the following code: int x=80; if (x>70){ x=40; }else if (x<90){ x=20; } if (x>30){ x=60; } a. 20 b. 40 c. 60 d. 80

C. 60

You may reason about the behavior of Java code involving immutable types exactly as if they were primitive types because: a. "Immutable" and "primitive" are synonyms; there is no difference between them b. Computations involving immutable types are just as efficient as those involving primitive types c. Aliasing, which can happen with immutable types but not primitive types, cannot cause trouble because object values for immutable types cannot be changed d. In any code where an immutable type is used in a way where it would not behave like a primitive type, it causes a Java compile-time error

C. Aliasing

if x is an int variable, when does the boolean expression evaluate to true? ((x%5!=0)&&(x%2!=0) E)When x is divisible by 5 or by 2 but not by both F)When x is divisible by 10 G)When x is not divisible by 10 H)when x is neither divisible by 5 nor by 2

H)when x is neither divisible by 5 nor by 2

The correct syntax for the "main" method signature is A) private static void main(String[] args) B) private static String main(Array[]args) C) private static void (String[]args) D) public static void(String[]args)

D) public static void(String[]args)

What is the value of s after the following statement: String s=(!true)+" : " + (10+4) + " is 104"; A)"!true : 104 is 104". B)"false : 104 is 104". C)"!true : 14 is 104". D)"false : 14 is 104". E) This is a compile-time error.

D)"false : 14 is 104".

Which would NOT be a food input value for a test case of this method? Decrements the given naturalNumber @updates n @requires n>0 @ensures n=#n-1 private static void decrement(NaturalNumber n) A)#n=1 B)#n=10 C)#n=42 D)#n=0

D)#n=0

What is the value of x after this statement double x=1/2; A)0.5 B)1.0 C)2.0 D)0.0

D)0.0

What is an appropriate method signature for a method p that will print a square of a dollar sign ($) with a given output stream, and a given size of the square. A)Private static SimpleWriter p(int size,"$") B)Private static void p(out,size) C)Private static int p(SimpleWriter out) D)Private static void p(SimpleWriter out,int size)

D)Private static void p(SimpleWriter out,int size)

What will be the value of array after this code executes? NaturalNumber[] array=new NaturalNumber2[4]; NaturalNumber count=new NaturalNumber2(1); for (int i=0; i<array.length; i++) { array[i].copyFrom(count); count.increment(); } A)array{0,0,0,0} B)array{1,2,3,4} C)array{4,4,4,4} D)There an error in the code that will cause a runtime error.

D)There an error in the code that will cause a runtime error.

Which statement correctly defines a Java constant? A)const SPECIAL=1234; B)int SPECIAL=1234; C)int final SPECIAL=1234; D)final int SPECIAL=1234;

D)final int SPECIAL=1234;

What line of code is missing from this method? private static int size(XMLTree t) { int totalnodes=1; xxxxxxxxx { for(int i=0;i<t.numberOfChildren();i++) {totalnodes+=size(t.child(i));} } return(totalnodes); A)if (t.numberOfChildren()>0){ B)if (!t.label().equals("")){ C)if (!t.isTag()){ D)if (t.isTag()){

D)if (t.isTag()){

In design-by-contract , when the precondition (requires clause) of a method is not satisfied the implementer of a method is obligated to: A) stop the program and report an error B)generate a default result that will let the client know of their error C)Make sure the postcondition (ensures clause) is satisfied D)none of the above

D)none of the above

NaturalNumber[] nums=new NaturalNumber[2]; nums[0]=new NaturalNumber1l(1); nums[1]=new NaturalNumber1l(2); mystery1(nums); private static void mystery1(NaturalNumber[] arr) { arr[0]=new NaturalNumber1l(2); arr[1].multiply(arr[0]); } A)nums[0]=1, nums[1]=2 B)nums[0]=2, nums[1]=2 C)nums[0]=1, nums[1]=4 D)nums[0]=2, nums[1]=4

D)nums[0]=2, nums[1]=4

Suppose youa re trying to get a good estimate of sqrt(x) using Newton iteration, as described in project 2. The instructions are to continue iterating until "estimate of square root of x is within relative error 0.01%". Which while loop condition would be appropriate? A)while(Math.abs(r-x)>=0.001){..} B)while(Math.abs(r-x)/x>=0.001){..} C)while(Math.abs(r*r-x)/x!=0.001){..} D)while(Math.abs(r*r-x)/x>0.001){..}

D)while(Math.abs(r*r-x)/x>0.001){..} ??

Instance methods may have any return type, including void T F

T

The reference value of a reference variable is related to the objects location in memory

T

Java byte code is saved in a A).class file B).java file C) .byte file D).exe file

a) .class file

Consider the following code fragment: SimpleWriter outStream = new SimpleWriter1L(); double threeQuarters= 3/4; outStream.println(threeQuarters); This code will output: a. 0.0 b. 0.75 c. 1.0 d. 3/4

a. 0.0

Consider the following method header: private static boolean isPrime(int x) and the call: boolean answer=isPrime(number); In this context: a. x is a formal parameter and number is an argument(or actual parameter) b. x is an argument (or actual parameter) and number is a formal parameter c. x is a local variable and number is a distinguished variable d. both x and number are global variables

a. x is a formal parameter and number is an argument(or actual parameter)

After execution of the following code, what are the declared type (static type) and the object type (dynamic type) of nCopy? NaturalNumber n = new NaturalNumber1L(); NauturalNumber nCopy=new NaturalNumber2(n); a. Declared type is NaturalNumber, object type is NaturalNumber1L b. Declared type is NaturalNumber, object type is NautralNumber2 c. Declared type is NaturalNumber2, object type is NaturalNumber1L d. Declared type is NaturalNumber2, object type is NaturalNumber

b. Declared type is NaturalNumber, object type is NautralNumber2

A type is an immutable type if: a. It is illegal to copy a reference of that type, so there can be no aliasing. b. For every instance method of that type, this and every other parameter of that type is a restores-mode parameter c. That type has a no-argument constructor. d. A function method may not return a result of that type.

b. For every instance method of that type, this and every other parameter of that type is a restores-mode parameter

Consider the following method header: private static void setToZero (int x) and the client code: int number=10; setToZero(number); The value of variable number after the method call: a. is guaranteed to be 0 b. is guaranteed to be 10 c. could be either 0 or 10, but nothing else d. could be anything; there is not enough information to know

b. is guaranteed to be 10

Assume n is an int variable. Which Java expression is true exactly when n is an odd number (not divisible by 2) and should be used to check for this situation? a. n%2==0 b. n%2!=0 c. n%2==1 d. n%2!=1

b. n%2!=0

In design-by-contract, the code responsible for making sure the precondition (requires clause) is true when a method is called is: a. the code that implements the method b. the client code that calls the method c. both the client and the implementation code d. neither the client nor the implementation code

b. the client code that calls the method

What is the value of nums[1] +nums[3] just after you declare: int[] nums={7,3,5,6}; a.12 b.9 c.4 d. It is impossible to tell

b.9

Suppose you want want to set teh double variable value to a random real number uniformly distributed in the interval [-2.0, 2.0). You have made the following declaration: Random r=new Random1L(); noting that r.nextDouble() returns a random real number uniformly distributed in the interval [0.0, 1.0). Which statement will set value to the desired result? a. value=-2.0 * r.nextDouble(); b. value=-2.0+2.0*r.nextdouble(); c. value= 4.0*r.nextDouble()-2.0; d. value=-2.0 *r.nextDouble() + 2.0*r.nextDouble()

c. value= 4.0*r.nextDouble()-2.0;

If a and b are boolean variables, when does the expression (!a||!b) evaluate to false? a. when either a or b is true, but not both b. when both a and b are false c. when both a and b are true d. when a is not equal to b

c. when both a and b are true

Which is true of JUnit testing when the unit under test is a single method? A. Very thorough JUnit testing can prove the correctness of the method body. b. JUnit testing is generally considered an expensive waste of time in industry c. A single JUnit test case can show the presence of a defect in the method body. d. A single JUnit test case can show the absence of defects in the method body, at least for the particular choice of inputs in that test case

c. A single JUnit test case can show the presence of a defect in the method body.

If you write class C implements interface I, the Java compiler checks: a. Whether I overrides all the methods of C b. Whether C inherits all the methods of I c. Whether each method in I has a method body in C d. Whether each method body in C is a correct implementation of the contract for that method in I

c. Whether each method in I has a method body in C

What are the values of array and index after the call to bar? private static void bar(int[] a, int i){ a[i]++; i++; } ... int[] array={1,2,3}; int index=1; bar(array,index); a. array={1,2,3), index=1 b. array={1,2,3), index=2 c. array={1,3,3), index=1 d. array={1,3,3), index=2

c. array={1,3,3), index=1

consider the following code fragment: SimpleWriter out=new SimpleWriter1L(); out.print("39"+3); This code will certainly: a. be illegal in Java (i.e., it's a compile time error) b. cause the program to crash when it is executed (i.e., it's a run-time error) c. print out 393 d. print out 42

c. print out 393

Which line of code could be the body of a method that reports whether a point with coordinates (x,y) is inside the square with sides of length 2 centered (1,1)? a. return(Math.abs(x)<=1.0 &&Math.abs(y)<=1.0); b. return(x<=2.0 && y<=2.0); c. return (Math.abs(x-1.0)<=1.0 && Math.abs(y-1.0)<==1.0) d. none of the above

c. return (Math.abs(x-1.0)<=1.0 && Math.abs(y-1.0)<==1.0)

what are the values of str and num after the call to foo? private static void foo(String s, NaturalNumber n){ s="Columbus, " +s; n=new NaturalNumber2(43210); } ...String str = "Ohio"; NaturalNumber num=new NaturalNumber2(314); foo(str,num); a. str="Ohio", num=314 b. str="Columbus, Ohio", num=314 c. str="Ohio", num=43210 d. str="Columbus, Ohio", num=43210

c. str="Ohio", num=314

Consider the following code: int temp=90; String s=""; if (temp>80){ s="Too hot!"; } if (temp<30){ s="Too Cold!; } else { s="Just right!"; } After the execution, the value of s is: a. "" b. "Too hot!" c. "Too Cold!" d. "Just right!"

d. "Just right!"

For which one of the following primitive types it is best practice not to use == to compare values for equality? a. boolean b. char c. int d. double

d. double

Which of the following test cases should not be performed? a. routine or otherwise trivially simple test cases b. test cases that would cause no change to parameters c. boundary cases, based on the variables passed in d. test cases which do not meet method preconditions

d. test cases which do not meet method preconditions


Related study sets

Series 63: Administrative Procedures (Ch. 4)

View Set

AP Gov Chapter 8 Practice AP Questions

View Set

BUSFIN 4217 Venture Capital Test One

View Set

Illuminate Assignment: Rome and Christendom

View Set