CS 0007 Midterm Exam
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CS 0007
Midterm Exam
1. [10 points] True or False. For each of the following write “true” or “false” on the right.
1 |
Variable declaration means the same as variable initialization. |
|
2 |
String is a primitive data type. |
|
3 |
Instantiation refers to implementing a method in a class. |
|
4 |
Return type is part of a method signature. |
|
5 |
The scope of a variable refers to the size of a variable. |
|
6 |
A default constructor does not have any inputs. |
|
7 |
Any number can be represented as a String variable. |
|
8 |
A void method has to return an integer number. |
|
9 |
If x is true and y is false, what is the value of (x && !y) |
|
10 |
If x is true and y is false, what is the value of ((!x || !y) && x) |
|
2. [10 points] For each of the following statements, circle the correct answer.
1 |
According to name conventions, the name of a variable should be |
MyVar |
my_var |
myVar |
MY_VAR |
2 |
According to name conventions, the name of a constant should be |
MyConst |
my_const |
myConst |
MY_CONST |
3 |
According to name conventions, the name of a method should be |
MyMethod |
my_method |
myMethod |
MY_METHOD |
4 |
Consider the code String s1=””; What is the value returned by s1.length(); |
0 |
empty |
“ ” |
null |
5 |
Consider the code String s2=”Hello”; What is the value returned by s2.substring(2); |
Hello |
ello |
llo |
lo |
3. [20 points] Implement the following method which checks if a String is a palindrome. It should return true if so and false if not:
public boolean isPalindrome(String input){
}
4. [20 points] Implement the following method which prints out the all the prime numbers between 2 and n. Assume the input is a positive integer >1:
public void printPrimes(int n){
}
5. [40 points] Use the following UML diagram and the partially completed code for the class Bottle. Complete the code.
a. [10 points] Add the fields.
b. [10 points] Implement the getter for the field filled.
c. [10 points] Finish the constructor Bottle(double, String). Make it verify that the unit is “oz” or “cc”, and sets it to “oz” if not.
d. [10 points] Finish the method fill(double amount).The input parameter amount represents the amount of liquid to be added to the bottle. The field filled represents the amount of liquid already in the bottle. Make the method verify that the bottle will not overflow. The bottle can be filled to capacity but no more.
public class Bottle{
//Fields go here.
public Bottle (double size, String unit){
//Finish this constructor.
}
public Bottle(double size){
this(size,"oz");
}
public Bottle(){
this(16.0,"oz");
}
public double getSize() {return size;}
public String getUnit() {return unit;}
//Implement a getter for filled here.
public void fill(double amount){
//Finish this method here.
}
}
2024-02-26