CS Stuff?

Ace your homework & exams now with Quizwiz!

Q: Consider the following variable declarations and initializations.\nint a = 2;\nint b = 6;\nint c = 3;\nWhat does the following evaluate to?\na < b != c < b\n

A: False\n

Q: Consider the following method, which is intended to return the index of the first negative integer in a given array of integers.\npublic int positionOfFirstNegative(int[] values)\n{\nint index = 0;\nwhile (values[index] >= 0)\n{\nindex++;\n}\nreturn index;\n}\nWhat precondition is needed on the values array so that the method will work as intended?\n\n

A: The array values must contain at least one negative integer.\n

Q: Consider the following code segment.\nint count = 0;\nint number = 20;\nwhile (number > 0)\n{\nnumber = number / 2;\ncount++;\n}\nWhat will be the value of count after executing the code segment?\n\n

A:5\n

Q: Consider the following expression.\n(3 + 4 == 5) != (3 + 4 >= 5)\nWhat value, if any, does the expression evaluate to?\n

A:True\n

How would you correctly declare a variable that can store a temperature rounded to the nearest tenth of a degree?

Double paitientTemp; (or any other name)

Consider the following code segment.\nSystem.out.print("Hello!");\nSystem.out.println("How ");\nSystem.out.print("are ");\nSystem.out.print("you?");\nWhat is printed as a result of executing the code segment?

Hello!How\nAre you?

Which of the following arithmetic expressions evaluates to 1 ?\nI. 2 / 5 % 3\nII. 2 / (5 % 3)\nIII. 2 / 5 + 1

II and III

Each of the following code segments is intended to print the word Hello. Which of the following code segments works as intended?\nI. System.out.print("Hello");\nII. System.out.print(Hello);\nIII. System.out.print(He);System.out.print(llo);

Only I

Consider the following code segment.\nSystem.out.println("W");\nSystem.out.println("X");\nSystem.out.print("Y");\nSystem.out.print("Z");\nWhat is printed as a result of executing the code segment?

W\nX\nYZ\

Consider the code segment below.\nint x = 10;\nint y = 20;\nSystem.out.print(y + x / y);\nWhat is printed as a result of executing the code segment?

20

Consider the following code segment.\nint a = 1;\nint b = 2;\nint c = 3;\nint d = 4;\ndouble x = a + b * c % d;\nWhat is the value of x when the code segment has been executed?

3.0

Q: Consider the following method.\npublic void adjust(double max, double min, double total, double n)\n{\ntotal = total - max - min;\nn = n - 2.0;\nSystem.out.println(total / n);\n}\nConsider the call adjust(25.0, 5.0, 60.0, 5.0), which appears in a method in the same class. What is printed as a result of the method call?\n\n

A: 10.0

Q: Consider the following code segment.\nint j = 1;\nwhile (j <= 5)\n{\nfor (int k = 4; k > 1; k--)\n{\nSystem.out.println("ha"); // line 6\n}\nj++;\n}\nHow many times will the print statement on line 6 execute?\n\n

A: 15\n\n

Q: Consider the following code segment, which is intended to store the sum of all multiples of 10 between 10 and 100, inclusive (10 + 20 + ... + 100), in the variable total.\nint x = 100;\nint total = 0;\nwhile( /* missing code */ )\n{\ntotal = total + x;\nx = x - 10;\n}\nWhich of the following can be used as a replacement for /* missing code */ so that the code segment works as intended? \na) X<100\nb) X<=100\nc) X>10\nd) X>=10\ne) X != 10\n\n

A: D\n

Q: Consider the following method.\npublic int[] transform(int[] a)\n{\na[0]++;\na[2]++;\nreturn a;\n}\nThe following code segment appears in a method in the same class as transform.\n/* missing code */\narr = transform(arr);\nAfter executing the code segment, the array arr should contain {1, 0, 1, 0}. Which of the following can be used to replace /* missing code */ so that the code segment works as intended?\nI. int[] arr = {0, 0, 0, 0};\nII. int[] arr = new int[0];\nIII. int[] arr = new int[4];\n\n

A: I and III\n

Q: Consider the following class definition.\npublic class BoolTest\n{\nprivate int one;\npublic BoolTest(int newOne)\n{\none = newOne;\n}\npublic int getOne()\n{\nreturn one;\n}\npublic boolean isGreater(BoolTest other)\n{\n/* missing code */\n}\n}\nThe isGreater method is intended to return true if the value of one for this BoolTest object is greater than the value of one for the BoolTest parameter other, and false otherwise. The following code segments have been proposed to replace /* missing code */.\nI. return one > other.one;\nII. return one > other.getOne();\nIII. return getOne() > other.one;\nWhich of the following replacements for /* missing code */ can be used so that isGreater will work as intended?\n\n

A: I, II, and III - all of them\n

Q: Consider the class definition below. The method levelUp is intended to increase a Superhero object's strength attribute by the parameter amount. The method does not work as intended.\npublic class Superhero\n{\nprivate String name;\nprivate String secretIdentity;\nprivate int strength;\npublic Superhero(String realName, String codeName)\n{\nname = realName;\nsecretIdentity = codeName;\nstrength = 5;\n}\npublic int levelUp(int amount) // line 14\n{\nstrength += amount; // line 16\n}\n}\nWhat changes should be made so that the levelUp method works as intended?\n\n

A: In line 14, levelUp should be declared as type void.\n

Q: Consider the following code segment.\nint[] arr = {1, 2, 3, 4, 5};\nfor (int x = 0; x < arr.length; x++)\n{\nSystem.out.print(arr[x + 3]);\n}\nBest describes the behavior of this code:\n\n

A: It will cause an ArrayIndexOutOfBoundsException\n

Q: Consider the following code segment.\nint[] arr = {1, 2, 3, 4, 5};\nfor (int x : arr)\n{\nSystem.out.print(x + 3);\n}\nBest describes the behavior of this code:\n

A: It will print 45678.\n

Q: Consider the following class definition. The method appendIt is intended to take the string passed as a parameter and append it to the instance variable str. For example, if str contains "week", the call appendIt("end") should set str to "weekend". The method does not work as intended.\npublic Class StringThing\n{\nprivate String str;\npublic StringThing(String myStr)\n{\nstr = myStr;\n}\npublic void appendIt(String s) // line 10\n{\nstr + s; // line 12\n}\n}\nWhat changes should be made so that the appendIt method works as intended?\n\n

A: Line 12 should be changed to str = str + s;.\n

Q: Consider the following code segment.\nfor (int i = 0; i < 5; i++) // Line 1\n{\nfor (int j = 0; j < 5; j++)\n{\nint k = i + j;\nSystem.out.print(k + " ");\n}\n}\nBest describe the result of changing i < 5 to i > 5 in line 1?\n\n

A: Nothing will be printed because the body of the outer for loop will not execute at all.\n

Q: Consider the following class declaration. The changeWeather method is intended to update the value of the instance variable weather and return the previous value of weather before it was updated.\npublic class WeatherInfo\n{\nprivate String city;\nprivate int day;\nprivate String weather;\n \npublic WeatherInfo(String c, int d, String w)\n{\ncity = c;\nday = d;\nweather = w;\n}\n \npublic String changeWeather(String w)\n{\n/* missing code */\n}\n}\nWhat should replace /* missing code */ so that the changeWeather method will work as intended?\n\n

A: String prev = weather;\nweather = w;\nreturn prev;\n\n

Q: Consider the following class definition.\npublic class Pet\n{\nprivate String name;\nprivate int age;\npublic Pet(String str, int a)\n{\nname = str;\nage = a;\n}\npublic getName()\n{\nreturn name;\n}\n}\nWhat correctly explains why this class definition fails to compile?\n\n

A: The accessor method is missing a return type.\n

Q: Consider the following class definition.\npublic class Gadget\n{\nprivate static int status = 0;\npublic Gadget()\n{\nstatus = 10;\n}\npublic static void setStatus(int s)\n{\nstatus = s;\n}\n}\nThe following code segment appears in a method in a class other than Gadget.\nGadget a = new Gadget();\nGadget.setStatus(3);\nGadget b = new Gadget();\nWhat best describes the behavior of the code segment?\n\n

A: The code segment creates two Gadget objects a and b. The class Gadget's static variable status is set to 10, then to 3, and then back to 10.\n

Q: Consider the following class declaration.\npublic class Student\n{\nprivate String firstName;\nprivate String lastName;\nprivate int age;\npublic Student(String firstName, String lastName, int age)\n{\nfirstName = firstName;\nlastName = lastName;\nage = age;\n}\npublic String toString()\n{\nreturn firstName + " " + lastName;\n}\n}\nThe following code segment appears in a method in a class other than Student. It is intended to create a Student object and then to print the first name and last name associated with that object.\nStudent s = new Student("Priya", "Banerjee", -1);\nSystem.out.println(s);\nWhat best explains why the code segment does not work as expected?\n\n

A: The code segment will compile, but the instance variables will not be initialized correctly because the variable names firstName, lastName, and age refer to the local variables inside the constructor.\n

Q: Consider the following class declaration.\npublic class Student\n{\nprivate String name;\nprivate int age;\npublic Student(String n, int a)\n{\nname = n;\nage = a;\n}\npublic boolean isOlderThan5()\n{\nif (age > 5)\n{\nreturn true;\n}\n}\n}\nWhat best describes the reason this code segment will not compile?\n\n

A: The isOlderThan5 method is missing a return statement for the case when age is less than or equal to 5.\n

Q: Consider the following code segment.\nfor (int j = 0; j < 4; j++)\n{\nfor (int k = 0; k < j; k++)\n{\nSystem.out.println("hello");\n}\n}\nBest explain how changing the inner for loop header to for (int k = j; k < 4; k++) will affect the output of the code segment?\n\n

A: The string "hello" will be printed four additional times because the inner loop will iterate one additional time for each iteration of the outer loop.\n

Q: Consider the following class definition.\npublic class ClassP\n{\nprivate String str;\npublic ClassP(String newStr)\n{\nString str = newStr;\n}\n}\nThe ClassP constructor is intended to initialize the str instance variable to the value of the formal parameter newStr. What best describes why the ClassP constructor does not work as intended?\n\n

A: The variable str should not be declared as a String in the constructor.\n

Q: Consider the following code segment.\nint n = 6;\nfor (int i = 1; i < n; i = i + 2) // Line 2\n{\nSystem.out.print(i + " ");\n}\nBest explain how changing i < n to i <= n in line 2 will change the result?\n\n

A: There will be no change to the program output because the loop will iterate the same number of times.\n

Q: Consider the following code segment.\nint a = 1;\nint b = 0;\nint c = -1;\nif ((b + 1) == a)\n{\nb++;\nc += b;\n}\nif (c == a)\n{\na--;\nb = 4;\n}\nWhat are the values of a, b, and c after this code segment has been executed?\n

A: a = 1, b = 1, and c = 0\n

Q: Consider the following code segment.\nint[] arr = {1, 2, 3, 4, 5};\nWhat 2 lines of code would correctly set the first two elements of array arr to 10 so that the new value of array arr will be {10, 10, 3, 4, 5} ?\n\n

A: arr[0] = 10;\narr[1] = 10;\n\n

Q: Consider the following code segment.\nint a = 1;\nwhile (a <= 2)\n{\nint c = 1;\nwhile (/* missing condition */)\n{\nSystem.out.print("*");\nc++;\n}\na++;\n}\nThe code segment is intended to print "******". What conditional can be used to replace /* missing condition */ so that the code segment works as intended?\n\n

A: c <= 3\n

Q: Consider the following code segment.\nfor (int k = 0; k < 4; k++)\n{\n/* missing loop header */\n{\nSystem.out.print(k);\n}\nSystem.out.println();\n}\nThe code segment is intended to produce the following output.\n0\n11\n222\n3333\nWhat code can be used to replace /* missing loop header */ so that the code segment will work as intended?\n\n

A: for (int h = k; h >= 0; h--)\n

Q: In the code segment below, assume that the int array numArr has been properly declared and initialized. The code segment is intended to reverse the order of the elements in numArr. For example, if numArr initially contains {1, 3, 5, 7, 9}, it should contain {9, 7, 5, 3, 1} after the code segment executes.\n/* missing loop header */\n{\nint temp = numArr[k];\nnumArr[k] = numArr[numArr.length - k - 1];\nnumArr[numArr.length - k - 1] = temp;\n}\nWhat can be used to replace /* missing loop header */ so that the code segment works as intended?\n\n

A: for (int k = 0; k < numArr.length / 2; k++)\n

Q: Consider the following code segment, which is intended to set the Boolean variable inRange to true if the integer value num is greater than min value and less than max value. Otherwise inRange is set to false. Assume that inRange, num, min, and max have been properly declared and initialized.\nboolean isBigger;\nboolean isSmaller;\nboolean inRange;\nif (num < max)\n{\nisSmaller = true;\n}\nelse\n{\nisSmaller = false;\n}\nif (num > min)\n{\nisBigger = true;\n}\nelse\n{\nisBigger = false;\n}\nif (isBigger == isSmaller)\n{\ninRange = true;\n}\nelse\n{\ninRange = false;\n}\nWhich of the following values of num, min, and max can be used to show that the code does NOT work as intended?\n

A: num = 50, min = 50, max = 50\n

Q: Consider the following class definition.\npublic class Person\n{\nprivate String name;\n/* missing constructor */\n}\nThe statement below, which is located in a method in a different class, creates a new Person object with its attribute name initialized to "Washington".\nPerson p = new Person("Washington");\nWhat can be used to replace /* missing constructor */ so that the object p is correctly created?\n\n

A: public Person(String n)\n{\nname = n;\n}\n\n

Q: Consider the following code segment, which is intended to print the maximum value in an integer array values. Assume that the array has been initialized properly and that it contains at least one element.\nint maximum = /* missing initial value */;\nfor (int k = 1; k < values.length; k++)\n{\nif (values[k] > maximum)\n{\nmaximum = values[k];\n}\n}\nSystem.out.println(maximum);\nWhat should replace /* missing initial value */ so that the code segment will work as intended?\n\n

A: values[0]\n

Q: Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5.\nint num = 12345;\nint sum = 0;\n/* missing loop header */\n{\nsum += num % 10;\nnum /= 10;\n}\nSystem.out.println(sum);\nWhat should replace /* missing loop header */ so that the code segment will work as intended?\n\n

A: while (num > 0)\n

Q: Consider the following code segment, which is intended to create and initialize the two-dimensional (2D) integer array num so that columns with an even index will contain only even integers and columns with an odd index will contain only odd integers.\nint[][] num = /* missing code */;\nHow could you replace /* missing code */ so that the code segment will work as intended?\n\n

A: {{0, 1, 2}, {4, 5, 6}, {8, 3, 6}}\n

Q: Consider the following code segment.\nString str = "AP-CSA";\nfor (int i = 0; i < str.length(); i++)\n{\nif (str.substring(i, i + 1).equals("A"))\n{\nSystem.out.print(i + " ");\n}\n}\nWhat is printed as a result of executing the code segment?\n\n

A:0 5\n

Q: Consider the following code segment.\nint m = 8;\nint n = 3;\nif (m + n > 10)\n{\nSystem.out.print(m + n);\n}\nif (m - n > 0)\n{\nSystem.out.print(m - n);\n}\nWhat, if anything, is printed as a result of executing the code segment?\n

A:115\n

Q: Consider the following code segment.\nint a = 1;\nString result = "";\nwhile (a < 20)\n{\nresult += a;\na += 5;\n}\nSystem.out.println(result);\nWhat, if anything, is printed as a result of executing the code segment?\n\n

A:161116\n

Q: Consider the following code segment.\nint[] arr = {4, 3, 2, 1, 0};\nint total = 0;\nfor (int k = 0; k <= total; k++)\n{\nif (arr[k] % 2 == 0)\n{\ntotal += arr[k];\n}\nelse\n{\ntotal -= arr[k];\n}\n}\nSystem.out.print(total);\nWhat, if anything, is printed as a result of executing the code segment?\n\n

A:1\n

Q: Consider the following method.\npublic static int mystery(String string1, String string2)\n{\nString temp = string1;\nint position = 0;\nint result = 0;\nwhile(temp.indexOf(string2) >= 0)\n{\nposition = temp.indexOf(string2);\nresult++;\ntemp = temp.substring(position + 1);\n}\nreturn result;\n}\nThe following code segment appears in another method in the same class.\nSystem.out.println(mystery("Mississippi", "si"));\nWhat, if anything, is printed as a result of executing the code segment?\n\n

A:2\n

Q: Consider the following code segment.\nint quant = 20;\nint unitPrice = 4;\nint ship = 8;\nint total;\nif (quant > 10)\n{\nunitPrice = 3;\n}\nif (quant > 20)\n{\nship = 0;\n}\ntotal = quant * unitPrice + ship;\nWhat is the value of total after this code segment has been executed?\n

A:68\n

Q: Consider the following code segment.\nint a = 100;\nwhile (a > 1)\n{\nSystem.out.println("$");\na /= 2;\n}\nHow many $'s are printed as a result of executing the code segment?\n\n

A:6\n

Q: Consider the following code segment.\nint k = 35;\nwhile (k >= 0)\n{\nSystem.out.println("X");\nk -= 5;\n}\nHow many times will the string "X" be printed as a result of executing the code segment?\n\n

A:8\n

Q: The following code segment prints one or more characters based on the values of boolean variables b1 and b2. Assume that b1 and b2 have been properly declared and initialized.\nif (!b1 || b2)\n{\nSystem.out.print("A");\n}\nelse\n{\nSystem.out.print("B");\n}\nif (!(b1 || b2))\n{\nSystem.out.print("C");\n}\nelse\n{\nSystem.out.print("D");\n}\nif (b1 && !b1)\n{\nSystem.out.print("E");\n}\nIf b1 and b2 are both initialized to true, what is printed when the code segment has finished executing?\n

A:AD\n

Q: Consider the following code segment.\nString first = new String("duck");\nString second = new String("duck");\nString third = new String("goose");\nif (first == second)\n{\nSystem.out.print("A");\n}\nelse if (second == third)\n{\nSystem.out.print("B");\n}\nelse if (first.equals(second))\n{\nSystem.out.print("C");\n}\nelse if (second.equals(third))\n{\nSystem.out.print("D");\n}\nelse\n{\nSystem.out.print("E");\n}\nWhat is printed as a result of executing the code segment?\n

A:C

Q: The array fruits is declared below.\nString [] fruits = {"apples", "bananas", "cherries", "dates"};\nWhich of the following code segments will cause an ArrayIndexOutOfBoundsException ?\nI.\nfor (int i = 0; i <= fruits.length; i++)\n{\nSystem.out.println(fruits[i]);\n}\nII.\nfor (int i = 0; i <= fruits.length - 1; i++)\n{\nSystem.out.println(fruits[i]);\n}\nIII.\nfor (int i = 1; i <= fruits.length; i++)\n{\nSystem.out.println(fruits[i - 1]);\n}\n\n

A:I only\n

Q: Consider the following code segment.\nboolean a = true;\nboolean b = false;\nSystem.out.print((a == !b) != false);\nWhat is printed as a result of executing this code segment?\n

A:True\n

Q: Consider the following method.\npublic String mystery(String word, int num)\n{\nString result = "";\nfor (int k = num; k >= 0; k--)\n{\nresult += word.substring(0, k);\n}\nreturn result;\n}\nWhat is returned as a result of the call mystery("computer", 3) ?\n\n

A:comcoc\n

Q: Consider the following code segment, which uses properly declared and initialized int variables x and y and the String variable result.\nString result = "";\nif (x < 5)\n{\nif (y > 0)\n{\nresult += "a";\n}\nelse\n{\nresult += "b";\n}\n}\nelse if (x > 10)\n{\nif (y < 0)\n{\nresult += "c";\n}\nelse if (y < 10)\n{\nresult += "d";\n}\nresult += "e";\n}\nresult += "f";\nWhat is the value of result after the code segment is executed if x has the value 15 and y has the value 5 ?\n

A:def\n

Q: Consider the following code segment.\nboolean a = true;\nboolean b = true;\nSystem.out.print((b || (!a || b)) + " ");\nSystem.out.print(((!b || !a) && a) + " ");\nSystem.out.println(!(a && b) && b);\nWhat output is produced when this code segment is executed?\n

A:true false false\n

Q: Consider the following code segment.\nString myString = new String("my string");\nString yourString = new String();\nyourString = "my string";\nboolean dotEquals = myString.equals(yourString);\nboolean equalsEquals = (myString == yourString);\nSystem.out.print(dotEquals + " " + equalsEquals);\nWhat is printed as a result of executing the code segment?\n

A:true false\n

Q: Consider the following code segment.\nString str1 = new String("Happy");\nString str2 = new String("Happy");\nSystem.out.print(str1.equals(str2) + " ");\nSystem.out.print(str2.equals(str1) + " ");\nSystem.out.print(str1 == str2);\nWhat is printed as a result of executing the code segment?\n

A:true true false\n

Q: Consider the following method.\npublic void printSomething (int num, boolean val)\n{\nnum--;\nSystem.out.print(val);\nSystem.out.print(num);\n}\nConsider the following code segment, which appears in a method in the same class as printSomething.\nprintSomething(1, true);\nprintSomething(2, true);\nWhat is printed as a result of executing the code segment?\n\n

A:true0true1

Consider the following code segment.\nSystem.out.print("Ready"); // Line 1\nSystem.out.print("Set"); // Line 2\nSystem.out.print("Go!"); // Line 3\nThe code segment is intended to produce the following output but may not work as intended.\nReady\nSet\nGo!\nWhich change, if any, can be made so that the code segment produces the intended output?

Changing print to println in lines 1 and 2

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to ππ (pi) times the square of the radius.\nThe code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized.\n/* missing code */\nSystem.out.print(volume);\nWhich of the following can be used to replace /* missing code */ so that the code segment works as intended?\nI. double baseArea = pi * r * r;double volume = baseArea * h;\nII. double volume = pi * r * r;volume = volume * h;\nIII. double volume = pi * r * r * h;

I, II, III

Consider the following code segment.\nint x = 10;\nint y = 20;\n/* missing code */\nSystem.out.print(top / bottom);\nWhich of the following replacements for /* missing code */ will cause an ArithmeticException to occur?\nI. int top = x - y;int bottom = y - x;\nII. int top = 2 * x;int bottom = y - top;\nIII. int top = x + y;int bottom = 2 * top;

II only

Consider the following code segment.\nSystem.out.println(hello); // Line 1\nSystem.out.print(world); // Line 2\nThe code segment is intended to produce the following output but does not work as intended.\nhello\nworld\nWhich of the following changes can be made so that the code segment produces the intended output?

Putting the strings in quotation marks

Consider the following code segment:\n/* data type 1 */ x = 0.5;\n/* data type 2 */ y = true;\nWhich of the following best describes the data types that should be used to replace\n/* data type 1 */ and /* data type 2 */ so that the code segment compiles without error?\n

X is a double, y is a boolean

Consider the following code segment.\nSystem.out.print("cat ");\nSystem.out.println("dog ");\nSystem.out.println("horse ");\nSystem.out.print("cow ");\nWhat is printed as a result of executing the code segment?

cat dog\nhorse\ncow

A teacher determines student percentages in a course as the points a student earns divided by the total points available in the grading period. Points are awarded only in whole number increments, but student percentages are to be stored as decimals.\nThe following code segment appears in a program used to compute student percentages. Points that a student earns are stored in pointsEarned, the total points available in the grading period are stored in totalPoints, and the student percentage is stored in percentage.\nint pointsEarned;\n/* missing code */\nWhich of the following is most appropriate to replace /* missing code */ in the program?

int totalPoints;\ndouble percentage;

Consider the following code segment.\nint x;\nint y;\nx = 3;\ny = /* missing expression */;\nx = 1 + 2 * y;\nSystem.out.print(x);\nSystem.out.println(y);\nWhat should be in the missing segment for the output to be 94?

x+1


Related study sets

Lifespan Final Review Chapter 7 - 15

View Set

Bio Practical 2 (behavior, climate change)

View Set

Health Insurance Policy Provisions

View Set

Joseph Stalin and Totalitarianism

View Set

Synonyms and Antonyms Vocabulary Test

View Set

Chap 14 Development through the Lifespan, 7e

View Set

EMS Chapter 2 - Workforce Safety and Wellness

View Set

New Deal - Relief, Recovery, Reform

View Set

International Political Economy- Chapter 1

View Set

Chapter 59: Caring for Clients with Disorders of the Bladder and Urethra

View Set

intro to human development (belsky): Test #3

View Set