JustAnswer



CMIS242 - Homework #1

Java Concepts – Liang Chapters 5-8

Total: 48 points - 5% of the final grade

Each question is 1 point

Due Date: February 7, 2010

----------------------------------------------------------------------------------------------------------------------------------------

1. Which of the following should be declared as a void method?

A. Write a method that prints integers from 1 to 100.

B. Write a method that converts an uppercase letter to lowercase and returns the result.

C. Write a method that checks whether an integer number is greater than another integer number and returns the larger number.

D. Write a method that returns a random integer from 1 to 100.

2. Suppose

static void nPrint(String message, int n) {

while (n > 0) {

System.out.print(message);

n--;

}

}

What is the printout of the call nPrint('a', 4)?

A. aaaa

B. invalid call

C. aaa

D. aaaaa

3. What is Math.rint(3.5)?

A. 4.0

B. 3.0

C. 5.0

D. 3

E. 4

4. Suppose

static void nPrint(String message, int n) {

while (n > 0) {

System.out.print(message);

n--;

}

}

What is k after invoking nPrint("A message", k)?

int k = 2;

nPrint("A message", k);

A. 3

B. 1

C. 0

D. 2

5. A variable defined inside a method is referred to as __________.

A. a block variable

B. a global variable

C. a method variable

D. a local variable

6. Suppose your method does not return any value, which of the following keywords can be used as a return type?

A. int

B. None of the above

C. double

D. public

E. void

7.When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

A. pass by name

B. pass by reference

C. pass by value

D. method invocation

8.Arguments to methods always appear within __________.

A. parentheses

B. quotation marks

C. curly braces

D. brackets

9.Analyze the following code:

public class Test {

public static void main(String[] args) {

System.out.println(xMethod(5, 500L));

}

public static int xMethod(int n, long l) {

System.out.println("int, long");

return n;

}

public static long xMethod(long n, long l) {

System.out.println("long, long");

return n;

}

}

A. The program does not compile because the compiler cannot distinguish which xmethod to invoke.

B. The program runs fine but displays things other than given in a and b.

C. The program displays long, long followed by 5.

D. The program displays int, long followed by 5.

10.Each time a method is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

A. a heap

B. an array

C. a stack

D. storage area

11.The signature of a method consists of ____________.

A. return type, method name, and parameter list

B. method name and parameter list

C. method name

D. parameter list

12. Suppose int[] numbers = new int[10], what is numbers[0]?

A. 10.

B. 0

C. null

D. undefined.

13. Suppose array a is int[] a = {1, 2, 3}, what is a[0] - a[2]?

A. 2

B. 3

C. -1

D. 1

E. -2

14. Given the following statement

int[ ] list = new int[10];

A. The array variable list contains nine values of type int.

B. The array variable list contains ten values of type int.

C. The array variable list contains a memory address that refers to an array of 10 int values.

D. The array variable list contains a memory address that refers to an array of 9 int values.

15. Consider the following statement:

int[] numbers = new int[10];

What is numbers.length?

A. 10.

B. 5

C. undefined.

D. The above statements are illegal.

16.Which of the following are valid array declarations?

A. char[] charArray = new char[26];

B. double[3] nums = {3.5, 35.1, 32.0};

C. int[] words = new words[10];

D. char[] charArray = "Computer Science";

17.Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = new int[5];

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

x[i] = i;

System.out.println(x[i]);

}

}

A. The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBounds exception.

B. The program has syntax error because i is not defined in the last statement in the main method.

C. The program displays 0 1 2 3 4.

D. The program displays 4.

18. Analyze the following code:

public class Test {

public static void main(String[] args) {

int[] x = {0, 1, 2, 3, 4, 5};

xMethod(x, 5);

}

public static void xMethod(int[] x, int length) {

for (int i = 0; i < length; i++)

System.out.print(" " + x[i]);

}

}

A. The program displays 0 1 2 3 4 5 and then raises a runtime exception.

B. The program displays 0 1 2 3 4.

C. The program displays 0 1 2 3 4 and then raises a runtime exception.

D. The program displays 0 1 2 3 4 5.

19. Consider the following code fragment:

int[] list = new int[10];

for (int i = 0; i = 0; i--)

System.out.print(list2[i] + " ");

}

}

A. 0 1 3.

B. 3 2 1

C. 0 1 2

D. 2 1 0

E. 1 2 3

23. Binary search can be applied on an unsorted list.

A. false

B. true

24. Which of the following declarations are correct?

A. int[] list = {1, 2, 3, 4};

B. int[4] list = new int{1, 2, 3, 4};

C. int[] list = new int{1, 2, 3, 4};

D. int[] list = new []{1, 2, 3, 4};

25. The array index of the first element in an array is 0.

A. true

B. false

26. The array index is not limited to int type.

A. true

B. false

27. If the parameter is an object, both formal parameter and actual parameter reference to the same object.

A. false

B. true

28. Which of the following are properties of a constructor?

A. A constructor is called using the new operator.

B. Constructors may be overloaded.

C. A constructor must have the same name as the class.

D. All of the above

29. The order of methods in a class is immaterial.

A. false

B. true

30. If the parameter is of a primitive type, both formal parameter and actual parameter reference to the same memory.

A. false

B. true

31. The default constructor has no arguments.

A. false

B. true

32. All local variables in a method have default values.

A. false

B. true

33. You can always use the default constructor even though the non-default constructors are defined in the class.

A. true

B. false

34. You can create an instance of the Math class.

A. false

B. true

35. Java assigns a default value to a data member of a class if the data is not initialized.

A. true

B. false

36. Array variable is a reference variable.

A. false

B. true

37. You use the _________ operator to access members of an object.

A. ()

B. .

C. *

D. %

38. "unhappy".substring(2) returns "happy".

A. false

B. true

39. The method equals, compareTo, charAt, and length is not in the _______ class.

A. Character

B. StringBuilder

C. StringBuffer

D. String

40. Which of the following statement is correct?

A. char a = ‘a’;

B. String s = 'r';

C. char c = "r";

D. int k = null;

41. "ab".compareTo("aB") is _________.

A. equal to 0

B. greater than 0

C. less than or equal to 0

D. less than 0

42. What is the print out of the following code?

String s = "Welcome to Java";

s.replaceAll("a", "BB");

System.out.println(s);

A. Welcome to JavBB

B. Welcome to Java

C. Welcome to JBBvBB

D. Welcome to JBBva

43. "abcdefgh".subString(1, 3) returns ________.

A. bcd

B. bc

C. cde

D. abc

44. What is the printout of the following code?

String s1 = "Welcome to Java";

String s2 = "Welcome to Java";

System.out.println("s1 == s2 is " + s1 == s2);

A. false

B. s1 == s2 is true

C. s1 == s2 is false

D. true

45. Assume that the ASCII code for character c is 99 and for a is 97. What is the printout of the following code?

System.out.println('a' + 'c');

A. ac

B. 196

C. 9799

D. a99

46. Closing a PrintWriter object ensures that the data in the buffer are sent to the file.

A. false

B. true

47. Which of the following is not an object?

A. new String("abc");

B. "abc"

C. 343

D. new Date()

48. You compare two strings s1 and s2 using ________.

A. pareTo(s2)

B. pare(s2)

C. compare(s1, s2)

D. compareTo(s1, s2)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download