CSE-1322 Test 2

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Given the following method: public static int recursive(int n,int m) { if(n<=1) { return 1; } else { return m+recursive(n/2,m*2); } } What is the value of x after the following call: int x = recursive(6,8);

25

What is the output of the following code? import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<Integer> myNumbers = new ArrayList<Integer>(); myNumbers.add(20); myNumbers.add(40); myNumbers.add(60); myNumbers.add(80); System.out.println(myNumbers.get(myNumbers.size()-3));}}

40

static int math(int x, int y) { return x+y; } static int math (int x) { return x*x; } static double math(double x, double y) { return x*y; } static S/string math (){ return "Why does this work?"; } MAIN ( ) { PRINT(math(7.5, 9.5)); }

71.25

What is the output of the following code: class Main { public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1; } public static void main(String[] args) { try { do_stuff(); } catch(IndexOutOfBoundsException e) { System.out.println("A"); } catch(RuntimeException e) { System.out.println("B"); } catch(Exception e) { System.out.println("C"); } } }

A

Which of the following statements is true?

A try/catch block should be used when reading and writing to a file.

Which line or lines of code below will cause compile errors? class Parent { public int x; } class Child extends Parent { public void doStuff() { System.out.println("I'm a child"); } } class Main { public static void main(String[] args) { Parent p1 = new Parent(); Child c1 = new Child(); Parent p2 = new Child(); p1.doStuff(); //Line 1 c1.doStuff(); //Line 2 p2.doStuff(); //Line 3 ((Child)p2).doStuff(); //Line 4 } }

Line 1 Line 3

A code segment calculates the average of values stored in integers v1, v2, and v3 and stores the result in avg, which is of type double. What kind of error is caused with this statement? Double avg = v1 + v2 + v3 / (double) 3;

Logic

What is the maximum number of constructors a class can have?

Many

Will the following code compile? abstract class A { public abstract int do_stuff1(); public abstract int do_stuff2();} class B extends A { @Overridepublic int do_stuff1() { return 4; } } class Main { public static void main(String[] args) { B myB = new B(); } }

No, class B has no concrete method do_stuff2(), but it should have one.

A private attribute in a class is accessible to

Only members of the same class

What is the output of the following code? class Main { public static void swap(String str1, String str2) { String holdString; holdString = str1; str1 = str2; str2 = holdString;} public static void main(String[] args) { String s1 = "Programming is"; String s2 = "The best"; swap(s1, s2); System.out.println(s1+" "+s2);} }

Programming is The best

Refer to the following recursive method. Public int mystery(int n) { If (n < 0) return 2; else return mystery(n - 1) + mystery(n - 3); } Which value is returned by the call mystery(3)?

12

What is the output of the following code: class Wrong extends Exception { public Wrong(String message) { super(message); } } class Main { public static void doStuff(int x) throws Wrong { if(x<0) { throw new Wrong("Too Low"); } else if(x==0) { System.out.println(x); } else { doStuff(x-1); System.out.println(x); } } public static void main(String[] args) { try { doStuff(3); System.out.println("---"); doStuff(-1); } catch(Exception e) { System.out.println(e.getMessage()); } } }

0 1 2 3 --- Too Low

Given the following method: public static int recursive(int n) { if(n==1) { return 1; } else { return recursive((n+1)/2) + recursive(n/2); } } What is the value of x following this call: int x = recursive(10);

10

After executing the following code, what will be the contents of A.txt? import java.io.*; import java.io.File; import java.io.IOException; class Main { public static void write_file(String filename, String line, int x) { try { File myFile=new File(filename); PrintWriter theFile = new PrintWriter(myFile); for(int i=0;i<x;i++) { theFile.println(line); } theFile.close(); } catch(IOException e) { System.out.println("Error reading file: "+e.getMessage()); } } public static void main(String[] args) { write_file("A.txt","Hi",3); write_file("A.txt","Hi",3); } }

A.txt will contain the word Hi on three lines

After executing the following code, what will be the contents of A.txt? import java.io.*; import java.io.File; import java.io.IOException; class Main { public static void write_file(String filename, String line, int x) { try { File myFile=new File(filename); PrintWriter theFile = new PrintWriter(myFile); for(int i=0;i<x;i++) { theFile.println(line); } theFile.close(); } catch(IOException e) { System.out.println("Error reading file: "+e.getMessage()); } } public static void main(String[] args) { write_file("A.txt","Hi",3); } }

A.txt will contain the word Hi on three lines

Which statement about an abstract class and interface is false?

All of the methods in both an abstract class and an interface are public

What is the output of the following code? class A { public void do_it() { System.out.println("A"); } } class B extends A { @Override public void do_it() { System.out.println("B"); } } class Main { public static void main(String[] args) { A myClass=new B(); myClass.do_it(); } }

B

In a file, only a comma can be used as a delimiter.

False

What is the output of the following code? class Main { public static void dostuff(int x, int y) { System.out.println("Hi from dostuff 1");} public static void dostuff(int x) { System.out.println("Hi from dostuff 2");} public static void dostuff(char x) { System.out.println("Hi from dostuff 3");} public static void dostuff(boolean x) { System.out.println("Hi from dostuff 4");} public static void main(String[] args) { dostuff(7,10);dostuff('?');dostuff(7);}} public static void Main (string[] args) { dostuff(7,10); dostuff('?'); dostuff(7);}}

Hi from dostuff 1 Hi from dostuff 3 Hi from dostuff 2

Assume you have a file called QuizGrades.csv with the following content: Student1,100,90,80,90,80 Student2,70,80,85,85,80 Student3,100,100,100,95,100 Student4,70,70,80,70,80 Student5,90,90,90,90,100 Which of the following lines is the correct way to open a text file such as the above for reading.

Java File myFile = new File("QuizGrades.csv"); Scanner myScan = new Scanner(myFile);

If you call do_stuff() from main, what if any exception will you see? public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1;

Java: ArrayIndexOutOfBoundsExceptionC#: IndexOutOfRangeException

Two methods that have the same name, but different parameters are said to be overloaded methods.

True

What is the output of the following code: class Main { public static void main(String[] args) { int[] myArray = new int[5]; try { for(int i=0;i<=5;i++) { myArray[i]=i; } System.out.println(myArray[3]); } catch(Exception e) { System.out.println("Something went wrong"); } } }

Something went wrong

Exception handling enables a method to throw an exception to its caller.

True

If a recursive method has no base case (bottom), or if the base case is never reached, it will become infinite and the result will be stack overflow error.

True

Recursion represents a powerful programming technique in which a method makes a call to itself from within its own method body.

True

The try-catch construct consists of one try block and one or more catch blocks. The try block contains the code that could throw exceptions.

True

What is the output of the following code? class Main { public static String question(String x) { if(x.length()==0) { return x; } else { return "X"+question(x.substring(0,x.length()-1)); } } public static void main(String[] args) { String y=question("ABCDE"); System.out.println(y); } }

XXXXX

Given the following code, which of the below definitions for a child class Raptors is valid: [Hint: Pay attention to the Access Modifiers of the attributes in Birds]

class Raptors extends Birds { private String raptortype; public Raptors(double w, String b, String n) { super(w,b,n); raptortype="owl";} }

Given the following code, which method will cause a compile error: abstract class A { public int do_stuff1() { return 3; } public abstract int do_stuff2() {}; public void do_stuff3() { return; } public abstract int do_stuff4(); }

do_stuff2()

Which of the following will successfully write the word Output to the file a.txt in the current working directory?

import java.io.*; class Main { public static void main(String[] args) { try { File myFile = new File("a.txt"); PrintWriter sr = new PrintWriter(myFile); sr.println("Output"); sr.close(); } catch(IOException e) { System.out.println(e.getMessage()); } } }

Which of the following are valid variable declarations: (Select ALL that are correct)

int a=7; long b=-2; //char c="Y"; float d=17.2f; double e=9; Java: boolean g=true;

Why does the following code not compile? public class XYZ { private int myNum; public XYZ() { myNum=0;} public XYZ(int n1) { myNum=n1;} } public class ABC extends XYZ { private int num2; public ABC() { myNum=0; num2=0; } public ABC(int n1) { myNum=n1; num2=n1*2; } } class Main { public static void main(String[] args) { ABC myABC = new ABC(); } }

myNum is defined as private in XYZ and as such is inaccessable in ABC.

A method that calls itself is said to be:

recursive

To successfully sum all integers in an array, what should the missing line of code be: public static int sum_array(int[] myArray,int start) { if(start>myArray.length-1) { return 0; } //What goes here? }

return(myArray[start]+sum_array(myArray,start+1));

Assume you have a file called a.txt with the following content : jane,100 tim,80 janet,40 What is the output of the following code: import java.util.Scanner; import java.util.ArrayList; import java.io.*; class Score { public String name; public int theScore; public Score(String name, int theScore) { this.name=name; this.theScore=theScore; } } class Main { public static void main(String[] args) { ArrayList<Score> scoreCard = new ArrayList<Score>(); try { File scoreFile = new File("a.txt"); Scanner sf = new Scanner(scoreFile); while(sf.hasNextLine()) { String line=sf.nextLine(); String[] parts=line.split(","); Score newScore = new Score(parts[0],Integer.parseInt(parts[1])); scoreCard.add(newScore); } } catch(IOException e) { System.out.println(e.getMessage()); } System.out.println(scoreCard.get(1).name); } }

tim

Given the following code: Java public static void check_age(int age) throws Exception { if(age<0) { throw new Exception("Age cannot be below 0"); } else if(age>140) { throw new Exception("That seems too high."); } }

try { check_age(age); } catch(Exception e) { System.out.println(e.getMessage()); }

What is the output of the following code? class Main { public static int dostuff(int y) { return y+=10; } public static void main(String[] args) { int x=15; for(int i=0;i<2;i++) { x=dostuff(x);} System.out.println("x:"+x);}}

x:35


Set pelajaran terkait

Chapter 27: Community Mental Health Nursing

View Set

Chapter 23 Holder in Due Course and Transferability

View Set

Econ 101 Ch. 2-3 Demand & Supply

View Set