CSCI 160 Midterm Review
-
Upload
brady-rogers -
Category
Documents
-
view
75 -
download
0
description
Transcript of CSCI 160 Midterm Review
![Page 1: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/1.jpg)
CSCI 160 Midterm Review
Rasanjalee DM
![Page 2: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/2.jpg)
Multiple Choice Questions
![Page 3: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/3.jpg)
Question
• The Java compiler translates Java programs into machine language programs.TRUE FALSE
![Page 4: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/4.jpg)
Answer
• The Java compiler translates Java programs into machine language programs.
• [FALSE]
![Page 5: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/5.jpg)
Question
• System.out is an example of a method.TRUE FALSE
![Page 6: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/6.jpg)
Answer
• System.out is an example of a method.TRUE FALSE
[FALSE]
![Page 7: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/7.jpg)
Question
• ice-cream is an illegal identifier in Java.• TRUE FALSE
![Page 8: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/8.jpg)
Answer
• ice-cream is an illegal identifier in Java.• TRUE FALSE• [TRUE]
![Page 9: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/9.jpg)
Question
• Constructors have return type void.• TRUE FALSE
![Page 10: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/10.jpg)
Answer
• Constructors have return type void.• TRUE FALSE• [FALSE]
![Page 11: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/11.jpg)
Fill in the Blanks
![Page 12: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/12.jpg)
Questions
• Every variable in Java must be __________ before it is used.
• Single quotes are used with constants of the
__________ type.
![Page 13: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/13.jpg)
Answers
• Every variable in Java must be __________ before it is used.[declared]
• 2. Single quotes are used with constants of the
__________ type.[char]
![Page 14: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/14.jpg)
Questions
• A(n) __________ is used for signaling the end of the input.
• A(n) __________ is an action that an object can take and is specified in the class definition.
• Compiling a file called Game.java will produce a file called __________.
![Page 15: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/15.jpg)
Answers
• A(n) __________ is used for signaling the end of the input.[sentinel value]
• A(n) __________ is an action that an object can take and is specified in the class definition.[method]
• Compiling a file called Game.java will produce a file called
__________.[Game.class]
![Page 16: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/16.jpg)
Question
• A(n) __________ is a data item that belongs to an object.
• __________ is the word used to say that there are no restrictions on the use of a method or data item can be used.
• The reserved word __________ can be used as a name for the calling the current object.
![Page 17: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/17.jpg)
Answer• A(n) __________ is a data item that belongs to an object.• [instance variable]
• 4. __________ is the word used to say that there are no restrictions on the use of a method or data item can be used.
• [public] • 5. The reserved word __________ can be used as a name
for the calling object.• [this]
![Page 18: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/18.jpg)
Short Answer Questions
![Page 19: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/19.jpg)
Question
• Add parentheses to the following expressions to indicate how Java will interpret them. (a) a * b * c - d / e(b) a + - b * c - d
![Page 20: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/20.jpg)
Answer
(a) ((a * b) * c) - (d / e)(b) (a + ((-b) * c)) - d
![Page 21: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/21.jpg)
Question
• After the following statements have been executed, how many Fraction objects will exist, not counting garbage objects?
Fraction f1 = new Fraction(1, 2);Fraction f2 = new Fraction(3, 5);Fraction f3 = f2;f2 = null;f1 = f2;
![Page 22: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/22.jpg)
Answer
• One
![Page 23: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/23.jpg)
Question• Answer the questions
below about this class.a) What are the names of the
instance variables declared in this class?
b) What are the signatures of the constructors declared in this class?
c) What are the names of the parameters declared in this class?
d) What are the signatures of the methods declared in this class?
e) What are the names of the constants declared in this class?
![Page 24: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/24.jpg)
Answer• Answer the questions below about this class.a) What are the names of the instance variables
declared in this class?name, slalomPoints, giantSlalomPoints, superGPoints, ussaNumber
b) What are the signatures of the constructors declared in this class?public Skier (String name, String ussaNumber)
c) What are the names of the parameters declared in this class?name, ussaNumber
d) What are the signatures of the methods declared in this class?public String getBestEvent()
e) What are the names of the constants declared in this class?MAXIMUM_POINTS
![Page 25: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/25.jpg)
Code Analysis
![Page 26: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/26.jpg)
Question
• What does the following program print?
int[] arr = {1, 1, 0, 0, 0};for (int i = 2; i < arr.length; i++)
arr[i] = arr[i-1] + arr[i-2];
![Page 27: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/27.jpg)
Answer
• What does the following program print? 11235index = 0 1 2 3 4
int[] arr = {1, 1, 0, 0, 0};for (int i = 2; i < arr.length; i++)
arr[i] = arr[i-1] + arr[i-2];
i arr[i-1] arr[i-2] arr[i] arr2 1 1 1+1 =2 112003 2 1 2+1 =3 112304 3 2 2+3 = 5 11235
![Page 28: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/28.jpg)
Question• Show the EXACT output the following programs generate:public class e21 {
public static void main (String args[]) {int num = 4;printNumbers(num);}
private static void printNumbers(int n) {for (int i=1; i <= n; i++) {for (int k=1; k <= 2*(i-1)+1; k++)System.out.print(i);System.out.println();}}
}
![Page 29: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/29.jpg)
Answer
1222333334444444
![Page 30: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/30.jpg)
Write Java Code
![Page 31: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/31.jpg)
Question
• Write one line java statement declaring and creating a 1-d double array that stores whether or not precipitation was recorded on each day of one (non-leap year)
![Page 32: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/32.jpg)
Answer
• boolean[] precipRecord = new boolean[365];
![Page 33: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/33.jpg)
Question
• Write a one-line java statement declaring and creating a 2-d array named matrix with 24 rows and 80 columns.
![Page 34: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/34.jpg)
Answer
• double[][] matrix = new double[24][80];
![Page 35: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/35.jpg)
Question
• Write 1 or more java statements to display all the elements of the last column of the 2-d array matrix from the previous part
![Page 36: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/36.jpg)
Answerfor(int i=0; i<matrix.length; i++)
System.out.println(matrix[i][79]);
![Page 37: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/37.jpg)
Question
• Write 1 or more java statements to compute and print the product of all the elements in the second row of the 2-d array matrix
![Page 38: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/38.jpg)
Answer
double product = 1for(int i=0; i<matrix.length; i++){
product *= matrix[1][i];}System.out.println(product);
![Page 39: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/39.jpg)
Question
• Write 1 or more java statements to compute and print the number of upper case characters in a saying (stored in variable saying)
![Page 40: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/40.jpg)
Answer
int upper= 0;for(int i=0;i<saying.length;i++){
if(saying.charAt(i)>=‘A’ && saying.charAt(i)<=‘Z’){
upper+;}
}System.out.println(upper);
![Page 41: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/41.jpg)
Question
• Write a method named toBinary that accepts an integer as a parameter and returns a string of that number's representation in binary. For example, the call of toBinary(42) should return "101010". We will assume a positive parameter.
![Page 42: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/42.jpg)
Answerprivate static String toBinary(int num) {
if (num == 0) return "0";
String str=""; while (num != 0)
{ str = (num%2)+str; num /= 2; } return str; }
![Page 43: CSCI 160 Midterm Review](https://reader036.fdocuments.net/reader036/viewer/2022062407/56812bdf550346895d904e69/html5/thumbnails/43.jpg)