Midterm Comp Sci A

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given the following: if (a > 0) if (b > 0) x = x + 5; else if (a > 5) x = x + 4; else x = x + 3; else x = x + 2; If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed? 0 2 3 4 5

3

Consider the following method: public void fun(int a, int b) { a += b; b += a; } What is output from the following code? int x = 3, y = 5; fun(x, y); System.out.println(x + " " + y); a) 3 5 b) 3 8 c) 3 13 d) 8 8 e) 8 13

3 5

A color image is broken down into individual pixels (points), each of which is represented by a 1 for white and a 0 for black 3 values denoting the shade of red, green, and blue in the image a single number indicating the intensity of color between white and black two numbers, a value that denotes where between white and black the color is, and a brightness none of the above, it is not possible to represent a color image

3 values denoting the shade of red, green, and blue in the image

What will be the largest value printed by the following code? for (int x = 5; x > 0; x--) for (int y = 0; y < 8; y++) System.out.println(x*y); 5 8 35 40 64

35

6 bits can be used to represent ______ distinct items or values 6 20 24 32 64

64

Consider the following code segment. public static int mystery(int n) { int x = 1; int y = 1; while (n > 2) { x = x + y; y = x - y; n--; } return x; } What value is returned as a result of the call mystery(6) ? a) 1 b) 5 c) 6 d) 8 e) 13

8

Consider the following method. public void test(int x) { int y; if (x % 2 == 0) y = 3; else if (x > 9) y = 5; else y = 1; System.out.println("y = " + y); } Which of the following test data sets would test each possible output for the method? 8, 9, 12 7, 9, 11 8, 9, 11 8, 11, 13 7, 9, 10

8, 9, 11

A URL (Universal Resource Locator) specifies the address of A computer on any network A computer on the Internet A local area network (LAN) on the Internet A document or other type of file on the Internet A Java program on the internet

A document or other type of file on the Internet

Since you cannot take the square root of a negative number, you might use which of the following instructions to find the square root of the variable x? Math.sqrt(x*x); Math.sqrt((int) x); Math.sqrt(Math.abs(x)); Math.abs(Math.sqrt(x)); Math.sqrt(-x);

Math.sqrt(Math.abs(x));

In order to a computer to be accessible over a computer network, the computer needs its own MODEM Communication Line Network Address Packet Router

Network Address

Volatility is a property of RAM ROM disk software computer networks

RAM

The following nested loop structure will execute the inner most statement (x++) how many times? for (int j = 0; j < 100; j++) for (int k = 100; k > 0; k--) x++; 100 200 10,000 20,000

10,000

In the following code, what value should go in the blank so that there will be exactly six lines of output? for (int x = 0; x < _____; x = x + 2) System.out.println("-"); 5 6 10 11 13

11

Which memory capacity if the largest? 1,500,000,000,000 bytes 100 gigabytes 3,500,000 kilobytes 10 terabytes 12,000,000 megabytes

12,000,000 megabytes

If x is an int where x = 1, what will x be after the following loop terminates? while (x < 100) x *= 2; 2 64 100 128 None of the above, this is an infinite loop

128

What is output with the statement System.out.println(x+y); if x and y are int values where x=10 and y=5? 15 105 10 5 x+y An error since neither x nor y is a String

15

Consider the following code segment. int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " x * y); else System.out.println("Value is: " x / y); What is printed as a result of executing the code segment? Value is: 21 Value is: 2.333333 Value is: 2 Value is: 0 Value is: 1

2

Consider the following method. private int swap(int a, int b) { if (a < b) { b = a; a = b; } return b - a; } What are the values of the variables a, b, and c after the following statements are executed? int a = 2, b = 5; int c = swap(a, b); 2, 5, 0 2, 5, 3 2, 5, -3 2, 2, 0 5, 2, 3

2, 5, 0

The expression a || b will be short-circuited if a is false. True False

False

Consider the following code: public static void main(String[] args) { System.out.print("Here"); System.out.println("There " + "Everywhere"); System.out.println("But not" + "in Texas"); } The program will print the word "Here" and then print what? "There Everywhere" on the line after "Here" "There" on the line after "Here" and "Everywhere" on the line after "There" "There Everywhere" on the same line as "Here" "ThereEverywhere" on the same line as "Here" "ThereEverywhere" on the line after "Here"

"There Everywhere" on the same line as "Here"

In the String major = "Computer Science", what is returned by major.substring(1, 2)? "C" "o" "m" 'C' "Computer"

"o"

Consider the following method. public String mystery(String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; } What is returned as a result of the call mystery("computer") ? "computer" "cmue" "optr" "ompute" Nothing is returned because an IndexOutOfBoundsException is thrown.

"optr"

Which of the following expressions does not evaluate to 0.4? (int) 4.0 / (double) 10; (double) (4 / 10); 4.0 / 10; 4 / 10.0; (double) 4 / (double) 10;

(double) (4 / 10);

Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use? (s1.equals(s2)) (s1.length().equals(s2)) (s1.length().equals(s2.length()) (s1.length() == s2.length()) length(s1) == length(s2)

(s1.length() == s2.length())

The instruction: System.out.println("Hello World"); might best be commented as: // prints "Hello World" to the screen // prints a message // used to demonstrate an output message // // meaningless instruction

// used to demonstrate an output message

Consider the following class. public class Counter { private int count = 0; public Counter() { count = 0; } public Counter(int x) { count = x; } // Line 1 public int getCount() { return count; } // Line 2 public void setCount(int c) { int count = c; } // Line 3 public void increment() { count++; } // Line 4 public String toString() { return "" + count; } // Line 5 } The test code Counter c = new Counter(); c.setCount(3); c.increment(); System.out.println(c.getCount()); is supposed to print 4, but the class has an error. What is actually printed, and which line in the class definition should be changed to get the correct output, 4? 0, Line 1 1, Line 3 0, Line 4 3, Line 4 36, Line 5

1, Line 3

Given the following code, where x = 0, what is the resulting value of x after the for-loop terminates? for (int i = 0; i < 5; i++) x += i; 0 4 5 10 15

10

How many times will the following loop iterate? int x = 10; while (x > 0) { System.out.println(x); x--; } 0 1 9 10 11

10

if you want to store into the String name the value "George Bush", you would use which statement? String name = "George Bush"; String name = new String("George Bush"); String name = "George" + " " + "Bush"; String name = new String("George" + " " + "Bush"); Any of the above would work

Any of the above would work

Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the following conditions: Condition 1: (x < y && x > 0) Condition 2: (a != d || x != 5) Condition 3: !(true && false) Condition 4: (x > y || a == 'A' || d != 'A') a) All four conditions are true b) Only condition 2 is true c) Condition 2 and Condition 4 are true only d) Conditions 2, 3, and 4 are all true, Condition 1 is not e) All 4 Conditions are false

Conditions 2, 3, and 4 are all true, Condition 1 is not

One can use a == to compare two Strings to see if they are the same. True False

False

Consider the following class: public class Clock { private int hours; private int mins; public Clock(int h, int m) { hours = h; mins = m; } // moves this clock one minute forward public void move() { < missing code > } public void set(int h, int m) { hours = h; mins = m; normalize(); } private void normalize() { while (mins >= 60) { mins -= 60; hours++; } hours %= 12; } } Which of the following could replace < missing code > in the move method? I. this = new Clock(hours, mins + 1); II. mins++; normalize(); III. set(hours, mins + 1); a) I only b) II only c) I and II only c) II and III only d) I, II, and III

II and III

Consider the following class. public class SomeMethods { public void one(int first) { /* implementation not shown */ } public void one(int first, int second) { /* implementation not shown */ } public void one(int first, String second) { /* implementation not shown */ } } Which of the following methods can be added to the SomeMethods class without causing a compile-time error? I. public void one(int value) { /* implementation not shown */ } II. public void one(String first, int second) { /* implementation not shown */ } III. public void one(int first, int second, int third) { /* implementation not shown */ } I only I and II only I and III only II and III only I, II, and III

II and III only

Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a student's test score. if (score >= 90) grade = 'A'; if (score >= 80) grade = 'B'; if (score >= 70) grade = 'C'; if (score >= 60) grade = 'D'; else grade = 'F'; Which statement is correct? This code will work correctly in all cases This code will work correctly on if grade >= 60 This code will work correctly only if grade < 60 This code will work correctly only if grade < 70 This code will not work correctly under any circumstances

This code will work correctly only if grade < 70

Any loop written using a for statement can be written using a while statement. True False

True

Can you write an if statement inside a while or for statement? Yes No It depends Only once in the same class

Yes

Which character below is not allowed in an identifier? $ (Dollar Sign) _ (Underscore) 0 (Zero) q (Letter Q) ^ (Carrot Sign)

^ (Carrot Sign)

An error in a program that results in the program outputting $100 instead of the correct answer, $250 is a programmer error a syntax error a run-time error a logical error a snafu

a logical error

Refer to the following class. public class Point { private int myX; private int myY; public Point() { myX = 0; myY = 0; } public Point(int x, int y) { myX = x; myY = y; } public int getX() { return myX; } public int getY() { return myY; } } Which of the following statements create a point with coordinates (0, 0)? I. p = new Point(); II. p = Point(0, 0); III. p = (0, 0); a) I only b) II only c) III only d) I and II only e) II and III only

a) I only

Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following? if (count != 0 && total / count > max) max = total / count; a) The condition short circuits and the assignment statement is not executed b) The condition short circuits and the assignment statement is executed without problem c) The condition does not short circuit causing a division by zero error d) The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error e)The condition will not compile because it uses improper syntax

a) The condition short circuits and the assignment statement is not executed

A car dealership needs a program to store information about cars for sale. For each car, they want to keep track of the following information: number of doors whether the car has air conditioning average number of miles per gallon (MPG) Which of the following is the best object-oriented program design? a) Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and double milesPerGallon. b) Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon. c) Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon. d) Use a class Car, with a subclass Doors, with a subclass AirConditioning, with a subclass MilesPerGallon. e) Use three classes: Doors, AirCondition, and MilesPerGallon, each with a subclass Car.

a) Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and double milesPerGallon.

Forgetting a semicolon in a Java programming will cause a) a syntax error b) a run-time error c) a logical error d) no error at all e) converting the statement into a comment

a) a syntax error

Which of the following would be a legal Java identifier? a) i b) class c) ilikeclass! d) idon'tlikeclass e) i-like-class

a) i

Given that s is a String, what does the following loop do? for (int j = s.length(); j > 0; j--) System.out.print(s.charAt(j-1)); a) it prints s out backwards b) it prints s out forwards c) it prints s out backwards after skipping the last character d) it prints s out backwards but does not print the 0th character e) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

a) it prints s out backwards

Consider the following statement: System.out.println("1 big bad wolf\t8 the 3 little pigs\n4 dinner\r2night"); This statement will output _______ lines of text. a) 1 b) 2 c) 3 d) 4 e) 5

b) 2

Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } } Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC? a) Make myC public b) Include the method: public int getC() { return myC; } c) Include the method: private int getC() { return myC; } d) Include the method: public void getC(int x) { x = myC; } e) Include the method: private void getC(int x) { x = myC; }

b) Include the method: public int getC() { return myC; }

Which of the following stores data in binary format? ROM RAM a hard disk a CD all of the above

all of the above

Consider the following code segment. String s = "ALASKA"; int index = s.substring(1, 4).indexOf("A"); What is the value of index? a) 0 b) 1 c) 2 d) 5 e) -1

b) 1

For which of the following pairs of values a and b does the expression (a > 20 && a < b) || (a > 10 && a > b) evaluate to true? a) 5 and 0 b) 5 and 10 c) 15 and 10 d) 15 and 20 e) None of these

b) 15 and 10

Of the following list, which one is not true regarding Java as a programming language? a) It is a relatively recent language, having been introduced in 1995 b) It is a language whose programs do not require translating into machine language before they are executed c) It is an object-oriented programming language d) It is a language that embraces the idea of writing programs to be executed using the World Wide Web e) All of the above are true

b) It is a language whose programs do not require translating into machine language before they are executed

Comments should a) rephrase the code it explains in English b) be insightful and explain what the instruction's intention is c) only be included in code that is difficult to understand d) be used to define variables whose names are not easy to understand e) all of the above

b) be insightful and explain what the instruction's intention is

Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0? a) if (x > 0) x++; else x--; b) if (x > 0) x++; else if (x < 0) x--; c) if (x > 0) x++; if (x < 0) x--; else x = 0; d) if (x == 0) x = 0; else x++; x--; e) x++; x--;

b) if (x > 0) x++; else if (x < 0) x--;

Consider the following method. public static String scramble(String word, int howFar) { return word.substring(howFar + 1, word.length()) + word.substring(0, howFar); } What value is returned as a result of the call scramble("compiler", 3) ? a) "compiler" b) "pilercom" c) "ilercom" d) "ilercomp" e) Nothing is returned because an IndexOutOfBoundsException is thrown.

c) "ilercom"

Which for loop is equivalent to this while loop? int y = 5; while (y >= 0) { System.out.println(y); y--; } a) for (int y = 0; y < 5; y++) System.out.println(y); b) for (int y = 5; y > 0; y--) System.out.println(y); c) for (int y = 5; y >= 0; y--) System.out.println(y); d) for (int y = 0; y > 5; y++) System.out.println(y); e) for (int y = 0; y > 5; y--) System.out.println(y);

c) for (int y = 5; y >= 0; y--) System.out.println(y);

The main method for a Java program is defined by a) public static void main() b) public static void main(String[] args); c) public static void main(String[] args) d) private static void main(String[] args) e) the main method could be defined as in a, c, or d, but not b

c) public static void main(String[] args)

If you want to output the text "hi there", including the quote marks, which of the following could do that? a) System.out.println("hi there"); b) System.out.println(""hi there""); c) System.out.println("\"hi there"); d) System.out.println("\"hi there\""); e) none, it is not possible to output a quote mark because it is used to mark the beginning and ending of the String to be output

d) System.out.println("\"hi there\"");

Of the following if statements, which one correctly executes three instructions if the condition is true? a) if (x < 0) a = b * 2; y = x; z = a - y; b) { if (x < 0) a = b * 2; y = x; z = a - y; } c) if { (x < 0) a = b * 2; y = x; z = a - y; } d) if (x < 0) { a = b * 2; y = x; z = a - y; } e) B, C, and D are all correct, but not A

d) if (x < 0) { a = b * 2; y = x; z = a - y; }

Suppose the isPrime method is defined: // Returns true if p is a prime, false otherwise // Precondition: p >= 2 public static boolean isPrime(int p) { < implementation not shown > } Given int n = 101; int sum = 0; Which of the following code segments correctly computes the sum of all prime numbers from 2 to 101, inclusive? a) while (n != 2) { n--; if (isPrime(n)) sum += n; } b) while (n >= 2) { n--; if (isPrime(n)) sum += n; } c) while (n != 2) { if (isPrime(n)) sum += n; n--; } d) while (n >= 2) { if (isPrime(n)) sum += n; n--; } e) while (n >= 2 && isPrime(n)) { sum += n; n--; }

d) while (n >= 2) { if (isPrime(n)) sum += n; n--; }

Consider the following class. public class Sphere { public static final double PI = 3.14159; public static double volume(int r) { return 4 / 3 * PI * Math.pow(r, 3); } } Which of the following statements about this code is true? a) The class will not compile because no constructors are defined. b) The class will not compile because PI cannot be declared public. c) The class will not compile because the volume method is declared static. d) Math.pow(r, 3) cannot be used because r is an int. e) The class compiles with no errors but the volume method returns a smaller value than the expected LaTeX: \frac{4}{3}\pi r^343πr3.

e) The class compiles with no errors but the volume method returns a smaller value than the expected LaTeX: \frac{4}{3}\pi r^343πr3.

Once we have implemented the solution, we are not done with the problem because a) the solution may not be the best (most efficient) b) the solution may have errors and need testing and fixing before we are done c) the solution may, at a later date, need revisiting to handle new specificiations d) the solution may, at a later date, need revising because of new programming language features e) all of the above

e) all of the above

What should be the last conditional statement in an if-else-if ladder? default else break case

else

Consider the following code segment. String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; < missing statement > System.out.println(abc.substring(k, k+1)); It is supposed to print a randomly chosen letter of the alphabet. Any of the 26 letters can be chosen with equal probability. Which of the following can replace < missing statement > for the code to work that way? int k = Math.random(25); int k = Math.random(0, 25); int k = Math.random(25) + 1; int k = Math.random(26) - 1; int k = (int) (26*Math.random());

int k = (int) (26*Math.random());

Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } } The following declaration appears in another class. SomeClass obj = new SomeClass(); Which of the following code segments will compile without error? int x = obj.getA() int x; obj.getA(x); int x = obj.myA; int x = SomeClass.getA(); int x = getA(obj);

int x = obj.getA()

When executing a program, the processor reads each program instruction from secondary memory (storage) the Internet registers stored in the processor main memory could be any of these

main memory

It is important to dissect a problem into manageable pieces before trying to solve the problem because a) most problems are too complex to be solved as a single, large activity b) most problems are solved by multiple people and it is easy to assign each piece to a separate person c) it is easier to integrate small pieces of a program into one program than it is to integrate one big chunk of code into one program d) our first solution may not solve the problem correctly e) all of the above

most problems are too complex to be solved as a single, large activity

The ability to directly obtain a stored item by referencing its address is known as random access sequential access read-only access fetch access volatility

random access

A Java program is best classified as: hardware software storage processor input

software

Consider the following class declaration. public class Person { private String myName; private int myYearOfBirth; public Person(String name, int yearOfBirth) { myName = name; myYearOfBirth = yearOfBirth; } public String getName() { return myName; ] public void setName(String name) { myName = name; } // There may be instance variables, constructors, and methods that are not shown. } Assume that the following declaration has been made. Person student = new Person("Thomas", 1995); Which of the following statements is the most appropriate for changing the name of student from "Thomas" to "Tom"? student = new Person("Tom", 1995); student.myName = "Tom"; student.getName("Tom"); student.setName("Tom"); Person.setName("Tom");

student.setName("Tom");

An if statement may be used to make a decision in a program. True False

true

If x is an int and y is a double, all of the following are legal except which assignment statement? y = x; x = y; y = (double) x; x = (int) y all of the above are legal

x = y;

What value will z have if we execute the following assignment statement? double z = 5 / 10; z will equal 0.0 z will equal 0.5 z will equal 5.0 z will equal 0.05 none of the above, a run-time error arises because z is a double and 5 / 10 is an int

z will equal 0.0


Kaugnay na mga set ng pag-aaral

Auditing - Chapter 12 - Audit Evidence: Confirmations

View Set

Take the Grammar Chapter 3 Quiz. | BJU English

View Set

Intercultural Communication Final Exam

View Set

4B Vocabulary Practice: Celebrating Holidays

View Set

NMNC 1210 Immunity and Sexuality

View Set

Diseases of the Gallbladder and Biliary Tract

View Set

intro module 2 exam--skin-wound care part one Downs/Julian

View Set

Psychology and Science Terms and Concepts

View Set