Computer Science II - Juniata College



Computer Science II100 pointsName __________Key________________Up to 5 points extra credit for attending 2/22 Career Fair (Signatures of representatives in directory)Fill in the blanks for this Java program that enters a list of doubles into an array that is instantiated with an interactively entered size. The code determines the maximum value and prints the list of numbers larger than the average.[20 pts]import java.util.__Scanner______;public class SimpleStuff{ /* Lab 0 Java P5 Author: LKRhodes 2/21/2019 */ public static _void__ __main_ (String [] args) { int size; //of array double num; //input numbers double sum, avg, nSmall; // and stats Scanner keyboard = new Scanner (System.in); System.out.println ("Find basic stats for n numbers."); System.out.print ("Enter n: "); size = keyboard.___nextInt____ (); _double_ [] nums = new double [__size__ ]; // input values and sum them sum = 0.0; for (int i=__0__; i < _size____; i__++__){ nums[i] = _keyboard_____ .nextDouble(); sum += ___nums[i]_____; } avg = sum / _size__; System.out.println("The sum is "+ _sum__ + "\nand the average is " + avg); nSmall = __0____ ; System.out.println("Number of values smaller than the average are: "); for (int i=__0__; i<___size_ ; i_++_){ if(nums[i] < __avg___ ){ //is it smaller? nSmall __++____ ; } } System.out.println(. __nSmall__ ); } } Miscellaneous, basic Java concepts. Matching. Place the letter in the blank that best matches or fills in the statement.[10 pts] _D__ The file Test1.java contains _?__B___ The file Test1.class contains _?__G___ class Test1a extends Test1 means _?__E___ The automatic recovery of memory used by objects no longer accessible through any references.__H__ and __I__ are Java primitive types.__S__ To compare two strings, use _?_.__J__ All classes are subclass of _?_.__R__ Good class style calls for you to define _?_ method to return a description of the object._K___ The operation that instantiates an object through a constructor and returns its address.constThe bytecodes of Test1Subclasses of Test1The source code of class Test1Garbage collectionTest1 is a subclass of Test1aTest1a is a subclass of Test1chardoubleObjectnewSuperclassString<=.println().equals().strcmp().toString().compareTo()Object-oriented programming design principles. (True/False)[12 pts]_T__ Good design of an Abstract Data Type hides its data structure organization by private access of its components and exposes its operations by public access._ F __ Most compact source code is a software goal in object-oriented class design._ T __ The result of a method call can have another method applied, as part of a cascade of method calls._ F __ The this keyword is always optional._ F __ A method of a subclass can never call an overridden superclass method. _ F __ The information hiding principle says the user of a class should know the details of its implementation while the implementer should know how the class will be used._ T __ A precondition defines what must initially hold true for a method to correctly carry out its task._ T __ A class may define multiple methods with the same name as long as the parameter set is different.__ T _ A protected instance variable is like a private variable, but is actually accessible by methods of any subclass.__ F __ An abstract class is exactly the same as an Interface._ F __ Deep copying is generally preferred for most objects because it’s most efficient._ T __ A reference variable may be declared as an Interface type but must be instantiated with a concrete class.Arrays. For this question just give segments of Java code. You need not write entire methods or classes.[15 pts total] Assume a collection of names is maintained as an array of String objects called names[ ] and an int variable nNames holds the logical size of the array.a. Determine if the array is full and potentially write a message that there is no room to add any more data. [3]if (nNames == names.length) System.out.println(“Array is full”);b. We want to insert a new string as its very first element of the array. Use a for loop to move all the names in the names array down one position. [5]for( int i = nNames; i>0; i--){ names [i] = names[i-1];}c. Insert an empty string in the first element of the array. [2]names[0] = “”;d. Use a loop to traverse the array names to find the longest name. Store the array index of that longest name in variable pos. [5]int pos = 0;for(int i=0; i<nNames; i++){ if ( names[i].length() > names[pos].length ) pos = i; }True/false on asymptotic dominance concepts on cost functions. Remember that higher dominance is not best.[10 pts]___ T ___ A cost function can relate running time to the number of simple statements (assignment, I/O, comparisons) executed given a data size.___ F ___ A cost function can be decreasing at times.__ T ____ Asymptotic dominance analysis considers when data sizes get large.___ F ___ For the definition of big-O, f(n) = O(g(n)), where C*g(n) >= f(n) for some n > n0, the variable C is either 1 or 2.__ F ___ f(n) = n3 + 32n2 + 30 = O(n2)__ F ___ f(n) = 2k3 + 24n2 - 5n + 100 = O(n3)__ T ___ f(n) = 16 log n + 5 n2 = O(n2)__ T ___ f(n) =2n +100n6 = O(2n)__ F ___ O(log n) is called exponential complexity.__ F ____ O(2n) is called quadratic complexity.Estimate the running times using the big-O notation for each of these code segments. The size of the data set is n. Assume all variables are integer and variables hold no values dependent on n.[10 pts] ____ O(n) ___for(j=n; j>1; j--){ 3 assignments and 1 output;} ____ O(n) ____for (i=0; i<n; i++) for(k=1; k<100; k++){ // 4 assignments // 2 square root function calls } ____ O(n2) ____for (i=0; i<n; i++) for(j=0; j<i; j++){ // 3 assignments } ___ O(n3) _____i = n;while(i>0){ for(j=0; j<=n; j++){ foo(j,n); //foo takes linear time } i = i-3;} _ O(n2 log n) __i = n;while(i>0){ if(i % 2 == 1){ // if i is odd for(j=i; j<=i+5; j++){ foolin(i,j); //foolin executes in linear time } } for(j=i; j<=n; j++){ foolog(j,n); //foolog executes in log time } i = i-1;}Rank the algorithm complexities from "fastest" to "slowest" (increasing dominance). 1 is fastest (good, low cost) and 6 is slowest (bad, high cost).[5 pts]O(n5)O(log n)O(2n)O(n)O(1)O(n3)526314True/false on the abstract data types of arrayLists and stacks.[10 pts]__ T __ Both arrayLists and stacks are linear structures.__ F __ Pushing an element onto a stack using an arrayList requires shift the elements down.__ T __ A pop operation can throw a StackUnderflowException.__ T __ An arrayList has no artificial bound on its size—it is dynamic.__ T __ ArrayLists and stacks can store collections of any class.__ T __ The top variable tracks the most recent element inserted into the stack.__ T __ The arrayList class tracks the number of elements in the collection and is accessible by a getter method.__ F __ Stacks will hold a deep copy of the object that is pushed.__ T __ Stacks are the choice data structure for the computer to track method calls and returns.__ F __ Pop will send the element to the garbage collector for deletion.Stack operations. Fill in the blanks in part of the ArrayStack class for set up and the pop operation.[8 pts]public class ArrayStack <T> // define and initialize the head pointer{ protected ____int______ top ; protected T [] stack; public ArrayStack(int size)// constructor {___top____ = -1 ; stack = new T [ __size____ ]; }//end constructor public T pop(){ if ( top == _size__ ) __throw__ new StackUnderflowException ("Stack is empty—cannot pop"); T temp = ____stack____ [ ____top_____ ]; stack [ _top_ -- ] = null ; return temp; }} ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download