Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. What is printed when the following statement is executed? System.out.println(17 / 5 % 3 + 17 * 5 % 3); 1 4 9 42

1. What is printed when the following statement is executed?

System.out.println(17 / 5 % 3 + 17 * 5 % 3);

  1. 1

  2. 4

  3. 9

  4. 42

  5. 42.5

2. Consider the following method.

public void process(String s) { s = s.substring(2, 3) + s.substring(1, 2) + s.substring(0, 1); }

What is printed as a result of executing the following statements (in a method in the same class)?

String s = "ABCD"; process(s); System.out.println(s);

  1. ABCD

  2. CBA

  3. CDBCA

  4. CDBCAB

  5. IndexOutOfBoundsException

3.

Suppose m is a two-dimensional array of size 4 by 4, with all its elements intialized to zeroes. What will be the values stored in m after fill(m) is called? The method fill is defined as follows:

public void fill(int[][] m) { int n = m.length; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) m[i][j] = 1; } }

  1. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  2. 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0

  3. 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0

  4. 1 1 1 0 1 1 1 0 1 1 1 0 0 0 0 0

  5. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

4.

Which of the following statements will result in a syntax error?

  1. String x = "123";

  2. Integer x = "123";

  3. Object x = "123";

  4. String[] x = {"123"};

  5. All of the above will compile with no errors.

  1. What is printed as a result of executing the following statements?

    double x = 2.5, y = 1.99; System.out.println((int)(x/y) + (int)(x*y));

    1. 0

    2. 3

    3. 4

    4. 4.0

    5. 5

  2. Which of the following expressions is true if and only if NOT all three variables a, b, and c have the same value?

    1. a != b && b != c

    2. a != b || b != c

    3. a >= b && b >= c && c >= a

    4. a > b || b > c || a > c

    5. !(a == b || b == c || a == c)

  3. What is the result when the following code segment is compiled and executed?

    int m = 4, n = 5; double d = Math.sqrt((m + n)/2); System.out.println(d);

    1. Syntax error "sqrt(double) in java.lang.Math cannot be applied to int"

    2. 1.5 is displayed

    3. 2.0 is displayed

    4. 2.1213203435596424 is displayed

    5. ArithmeticException

  4. For which of the following pairs of values a and b does the expression

    (a > 20 && a < b) || (a > 10 && a > b)

    evaluate to true?

    1. 5 and 0

    2. 5 and 10

    3. 15 and 10

    4. 15 and 20

    5. None of the above

  1. What is printed as a result of executing the following code segment?

    List lst = new ArrayList(); for (int k = 1; k <= 6; k++) lst.add(new Integer(k)); for (int k = 0; k < 3; k++) { Integer i = lst.remove(k); lst.add(i); } for (Integer i : lst) System.out.print(i)

    1. 123456

    2. 456123

    3. 456321

    4. 246135

    5. IndexOutOfBoundsException

  2. Which of the following methods are equivalent (always return the same value for the same values of input parameters)?

    public boolean fun(int a, int b, int c) { if (a >= b) if (b >= c) return true; else return false; else return false; }

    public boolean fun(int a, int b, int c) { if (a >= b && b >= c) return true; else return false; }

    public boolean fun(int a, int b, int c) { return a >= b || b >= c; }

    1. I and II only

    2. I and III only

    3. II and III only

    4. All three are equivalent

    5. All three are different

  3. Consider the following class.

    public class Matrix { private int[][] m; /** Initializes m to a square n by n array with all * the elements on the diagonal m[k][k] equal to 0 and * all other elements equal to 1 */ public Matrix(int n) { m = new int[n][n]; < missing code > } < other constructors and methods not shown > }

    Which of the following could replace < missing code > in Matrix's constructor, so that it compiles with no errors and works as specified?

    for (int r = 0; r < n; r++) for (int c = 0; c < n; c++) m[r][c] = 1; for (int k = 0; k < n; k++) m[k][k] = 0;

    for (int k = 0; k < n; k++) m[k][k] = 0; for (int r = 0; r < n; r++) for (int c = 0; c < n; c++) if (r != c) m[r][c] = 1;

    for (int c = 0; c < n; c++) for (int r = 0; r < n; r++) if (r == c) m[r][c] = 0; else m[r][c] = 1;

    1. I only

    2. II only

    3. I and II only

    4. II and III only

    5. I, II and III

  4. Which of the following statements about developing a Java program is FALSE?

    1. The main purpose of testing a Java program is to make sure it doesn't generate run-time exceptions.

    2. Hello.class, a Java class file obtained by compiling Hello.java under a Windows operating system can be executed on a Mac computer.

    3. The size of an int variable in Java is four bytes, the same under any operating system and computer model.

    4. At run time, int values are represented in the computer memory in the binary number system.

    5. Division of an integer value by zero in a Java program will cause a run-time ArithmeticException.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

Showing the details, develop 1, x, x 2 , x 3 , x 4

Answered: 1 week ago