java Hw

profileAram
CIS210_midterm.pdf

Page 1

CIS210 Midterm Exam Fall 2020

Your Name: Grade:

The exam period is 75 minutes. The exam two types of questions: True/False

and Multiple Choice. There are 40 questions and each question is worth 2.5

points. Please answer A (T) or B (F) for True or False questions, and write

down the best answer (A\B\C\D) for multiple choice questions.

Section I: True or False

1. A constructor is a special Class member method. It is automatically called

when an object of the class is created. It can also be called more than once after

the object is created.

2. In Java, the new operator is used to create an instance/object of a class in the heap.

3. A reference variable is a memory location contains an address of an object stored in the stack.

4. Encapsulation and abstraction are both the pillars of Object-Oriented Programming. Abstraction means that a programmer hides all but the relevant

data about an object in order to reduce the complexity.

5. The body of a while loop repeats as long as the loop condition is false, whereas the body of a do while loop repeats as long as the loop condition it true

6. Class member variables should be declared as private because the object- oriented programming principle of encapsulation.

7. The method

public char getChar();

will return a character data.

Page 2

8. Assume that there is a Class named Stock and the class has a constructor

private Stock(String symbol, double price);

So a programmer can create a Stock object by doing the following in the

main method:

Stock stock = new Stock(“Google Inc”, 578.45);

9. A breakpoint is a marker that you can set to specify where execution should

pause when you are running your Java application in Eclipse.

10. We use int [] numbers to create a number of integer values in the heap.

Section II: Multiple choices

11. What is output by the following code segment?

boolean isHighTemp = true;

double degree = 100.01;

if (isHighTemp)

if(degree >= 110)

System.out.print(“Extremely Hot”);

else System.out.println(“Not Hot”);

A) Extremely Hot

B) Not Hot

C) Hot

D) Extremely Hot Not Hot

12. To produce the output 2 4 6 8 10, what is the loop condition for the following

loop?

int n = 0;

Page 3

do

{

n = n + 2;

System.out.print(n + “ ”);

}while (???)

A) n < 10

B) n <= 10

C) n < 8

D) n >= 2

13. In Java, the value of the expression 23 % 6 is:

A) 5

B) 2

C) 3

D) 4

14. Given the constant declaration

final int CAPACITY = 10;

which of the following is NOT a valid use of CAPACITY?

A) CAPACITY += CAPACITY;

B) CAPACITY = 100;

C) int currentSize = CAPACITY + 9;

D) A and B above

15. Given the following declaration and the method header for the Max method

double x, y, z;

Page 4

double Max(double, double);

which of the following shows a syntactically invalid call to Max method:

A) z = Max(x, y);

B) z = Max((x+y+z);

C) System.out.print(Max(x, (y+x) ));

D) z = y + Max((y-x), (x+y));

16. What is printed when the following code segment is executed?

int i, j;

for(i = 1; i <= 3; i++)

for( j = 1; j <= 2; j++)

System.out.print(i*j);

A) 643214

B) 123246

C) 224466

D) 122436

17. Given the following

int [] numbers = {3,5,7,9,13};

for( int i = 0; i < numbers.length; i = i + 3)

{

System.out.print(numbers[i]);

}

Page 5

What is the output?

A) 3713

B) 35913

C) 3913

D) None of above

18. Given the following statements

int n = 5;

switch(n)

{

case 3: System.out.println(n++);

break;

case 5: System.out.println(++n);

break;

case 7: System.out.println(n);

break;

}

What is the output?

A) 3

B) 4

C) 5

D) 7

19. Given the following statements

Page 6

Scanner input = new Scanner(System.in);

int x = 0, y = 0;

int sum = 0;

double result = 0.0;

for (float counter = 0.1f; counter < 1.0f; counter += 0.01f){

x = input.nextInt();

y = input.nextInt();

sum = sum + x + y;

}

result = sum % y;

System.out.print(result);

What is(are) the problem(s) of this program in terms of secure coding standards?

A) Use float as loop counter

B) Integer overflow

C) Divide-by-zero

D) All of above

20. Given the following statement

int x = 3, y = 6;

double z;

z = x / (double) y;

System.out.print(z);

What is the output?

Page 7

A) 0.5

B) 0

C) 0.0

D) 2.0

21. Which of the following does NOT calculate the arithmetic average of x, y, and

z. Assume that average is declared as double and x, y, z are declared as int.

A) average = (x + y + z) / (double) 3;

B) average = (x+y+z) / 3.0;

C) average = (double ) (( x+y+z) / 3);

D) average = (double) (x+y+z) / 3;

Consider the following Java program and answer questions:

public class Test{ //line 1

pubic static void main(String[] args){ //line 2

int x = 3; //line 3

int y = 6; //line 4

System.out.print(x); //line 5

int z = Change(x, y); //line 6

System.out.println(x); //line 7

System.out.println(y); //line 8

}

public static int Change(int x, int y) //line 10

Page 8

{

int z = 0; //line 11

x++; //line 12

z = x + y; //line 13

System.out.println(x); //line 14

return z; //line 15

}

}

22. What is the output on line 7?

A) 9

B) 6

C) 3

D) None of above

23. What is the output on line 8?

A) 3

B) 6

C) 9

D) None of above

24. What is the output on line 14?

A) 3

B) 6

C) 9

Page 9

D) None of above

Consider the following Java program and answer questions:

public class Test{ //line 1

public static void main(String[] args){ //line 2

String city = “Clayton”; //line3

String state = “MO”; //line 4

System.out.print(city + “,” + state ); //line 5

ChangeAddress(city, state); //line 6

System.out.println(city); //line 7

System.out.println(state); //line 8

}

public static void ChangeAddress(String c, String s) //line 10

{

c = “Tallahassee”; //line 11

s = “FL”; //line 12

String address = c + “,” + s; //line 13

System.out.println(address); //line 14

System.out.println(c); //line 15

}

}

25. What is the output on line 5?

A) Clayton,MO

Page 10

B) MO,Clayton

C) Clayton

D) None of above

26. What is the output on line 7?

A) Clayton

B) Tallahassee

C) FL

D) None of above

27. What is the output on line 8?

A) Clayton

B) Tallahassee

C) MO

D) None of above

28. What is the output on line 14?

A) Clayton

B) Tallahassee

C) FL

D) Tallahasse,FL

29. What is the output on line 15?

A) Clayton

B) Tallahassee

C) FL

Page 11

D) Tallahassee,FL

Consider the following Java program and answer questions:

public class BankAccount

{

private double balance;

private String accountName;

public BankAccount()

{

balance = 0.0;

accountName = “Test Account”;

}

public BankAccount(double startBalance)

{

balance = startBalance;

accountName = “Test Account”;

}

public BankAccount(double startBalance, String accountNickName)

{

balance = startBalance;

accountName = accountNickName;

}

//Assume all the getters and setters are implemented.

Page 12

}

public class AccountTest{

public static void main(String [] args){

BankAccount checkingAccount = new BankAccount();

BankAccount savingAccount = new BankAccount(5000);

BankAccount autoAccount = new BankAccount(1000, “Auto”);

System.out.println(checkingAccount.getBalance()); //line 1

System.out.println(checkingAccount.getAccountName()); //line 2

System.out.println(savingAccount.getBalance()); //line 3

System.out.println(savingAccount.getAccountName()); //line 4

System.out.println(autoAccount.getBalance()); //line 5

System.out.println(autoAccount.getAccountName()); //line 6

checkingAccount.setBalance(400);

checkingAccount.setAccountName(“Checking Account”);

System.out.println(checkingAccount.getBalance()); //line 7

System.out.println(checkingAccount.getAccountName()); //line 8

}

}

30. What is the output on line 1 and 2 respectively?

A) 0.0, Test Account

B) 5000.0, Test Account

C) 1000.0, Test Account

Page 13

D) None of above

31. What is the output on line 3 and 4 respectively?

A) 0.0, Test Account

B) 5000.0, Test Account

C) 1000.0, Test Account

D) 1000.0, Auto

32. What is the output on line 5 and 6 respectively?

A) 0.0, Auto

B) 5000.0, Auto

C) 1000.0, Test Account

D) 1000.0, Auto

33. What is the output on line 7 and 8 respectively?

A) 400.0, Auto

B) 5000.0, Checking Account

C) 400.0, Checking Account

D) None of above

34. If a program contains semantic errors, but no syntax errors, then we know that:

A) The compiler will not generate any Java byte code, so we can run the program,

but it may not work correctly and may not produce correct results

B) The compiler will generate Java byte code, so we can run the program and it

will work correctly and give correct results

C) The compiler will generate Java byte code, so we can run the program, but it

may not work correctly and may not produce correct results

Page 14

D) The compiler will not generate any Java byte code, so we cannot run the

program

Consider the following Java program and answer questions:

public static void main(String[] args){ ArrayList<Integer> numbers = new ArrayList<Integer>(100); //line 1 numbers.add(8); //line 2 numbers.add(3); //line 3 numbers.add(4); //line 4 numbers.add(2, 5); //line 5 numbers.add(2, 6); //line 6 numbers.add(9); //line 7 numbers.add(2,10); //line 8 System.out.println(numbers.get(2)); //line 9 System.out.println(numbers.get(4)); //line 10 System.out.println(numbers.get(10)); //line 11 System.out.println(numbers.size()); //line 12 numbers.set(2, 4); //line 13 numbers.remove(3); //line 14 numbers.remove(4); //line 15 System.out.println(numbers.get(2)); //line 16 System.out.println(numbers.get(4)); //line 17 for(Integer i : numbers){ //line 18 System.out.print(i); //line 19

}

}

35. What is the output on line 9?

A) 3

B) 5

C) 10

D) None of above

36. What is the output on line 10?

A) 3

B) 4

Page 15

C) 5

D) None of above

37. What is the output on line 11?

A) 3

B) 4

C) 5

D) None of above

38. What is the output on line 12?

A) 7

B) 4

C) 9

D) None of above

39. What is the output on line 16?

A) 7

B) 4

C) 9

D) None of above

40. What is the output of the for loop on line 18 - 19?

A) 83659

B) 83459

C) 84465

D) None of above