Question

In: Computer Science

1) What's the result of calling method blitz passing strings "Aquamarine" as the first argument and...

1)

What's the result of calling method blitz passing strings "Aquamarine" as the first argument and "Heliotrope" as the second argument?

static int blitz(String v, String w) {
    if (v.length() != w.length())
        return 0;
    int c = 0;
    for (int i = 0; i < v.length(); i++)
        if (v.charAt(i) == w.charAt(i))
            c++;
    return c;
}

a)0

b)1

c)2

d) 3

2)What is NOT an advantage of dynamic arrays compared to static arrays?

a)new elements can be added

b)elements can be inserted

c)elements can be individually indexed

d)elements can be removed

3)

Consider the following code segment:

ArrayList<Integer> scores = new ArrayList<>();

scores.add(5);

scores.add(8);

scores.add(1);

scores.add(1, 9);

scores.remove(2);

scores.add(0, 2);

What's the content of array scores after the code is executed?

a)[2, 5, 8, 1]

b)[2, 5, 9, 1]

c)[5, 2, 9, 1]

d) something else

4)

Consider the following code segment:

int mat[][] = new int[4][3];
for (int i = 0; i < mat.length; i++)
  for (int j = 0; j < mat[0].length; j++)
    if (i % 2 == 0)
      mat[i][j] = 2;
    else if (i % 3 == 0)
      mat[i][j] = 3;
    else
      mat[i][j] = 0;

What is the content of mat's 3rd row after the code segment is executed?

a)[3, 3, 3, 3]

b)[3, 3, 3]

c)[2, 2, 2, 2]

d)[2, 2, 2]

Solutions

Expert Solution

check out the solution.

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

1)

Answer - c) 2

Explanation:
A q u a m a r i n e
H e l i o t r o p e

A == H - false - c=0
q == e - false - c=0
u == l - false - c=0
a == i - false - c=0
m == o - false - c=0
a == t - false - c=0

r == r - true - c=1

i == o - false - c=0
n == p - false - c=0

e == e - true - c=2

therefore : c = 2

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

2)

Answer - b) elements can be inserted

Explanation : Memory leak issue, in a scenario where memory is full and dynamic arrays try to insert elements then an error is raised and cause exception.So inserting elements in dynamic arrays is a disadvantage over static arrays.

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

3)

Answer - b) [2, 5, 9, 1]

Explanation:

scores.add(5); // [5] - added
scores.add(8); // [5, 8] - added
scores.add(1); // [5, 8, 1] - added
scores.add(1, 9); // [5, 9, 8, 1] - added 9 at index 1
scores.remove(2); // [5, 9, 1] - removed element at index 2 so element 8 is removed
scores.add(0, 2); // [2, 5, 9, 1] - add 2 at index 0

Therefore answer is [2, 5, 9, 1]

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

4)

Answer - d) [2 2 2]

Explanation:

1st row - for i=0 - 0%2 == 0 - true - entire row is 2 - [2 2 2]
2nd row - for i=1 - 1%2 == 0 - false
- 1%3 == 0 - false
- else - entire row is 0 - [0 0 0]
3rd row - for i=2 - 2%2 == 0 - true - entire row is 2 - [2 2 2]
4th row - for i=3 - 3%2 == 0 - false
- 3%3 == 0 - true - entire row is 3 - [3 3 3]


therefore mat's 3rd row is - [2 2 2]

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

Test Programs in Java along with the output.

1.

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

2.

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

3.

===========================================


Related Solutions

Write a method that returns the result when the calling object is multiplied by a scalar...
Write a method that returns the result when the calling object is multiplied by a scalar value. For example, the PolyTerm 2.4x^3 multiplied by -1.2 should return the PolyTerm object representing -2.88x^3. Language: Java. Method name be like: scalarMultiply(double) Some Outputs: Test 1: Coefficient =1, Exponent = 1 scalarMultiply(1.2).coefficient return 1.2; scalarMultiply(1.2).exponent returns 1. Test 2: Coefficient =2.4, Exponent = 3 scalarMultiply(-1.2).coefficient returns -2.88 scalarMultiply(-1.2).exponent return 3 Test 3: Coefficient =-1.5 Exponent = 0 scalarMultiply(0).coefficient returns 0 scalarMultiply(0).exponent returns 3...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Write a method that takes four strings as parameter. The first string should be a pokemon...
Write a method that takes four strings as parameter. The first string should be a pokemon name, the second a pokemon type, the third a pokemon name, and the fourth a pokemon type. The method should print out which pokemon has the advantage over the other based on their type. In this case you can use fire, water, and leaf. Example outputs should be like: Example: Pokemon X (Water) has the advantage over Pokemon Y (fire) Pokemon X (Fire) has...
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
1.        In your program, demo the split() method for strings and attach it to one of the...
1.        In your program, demo the split() method for strings and attach it to one of the buttons. "Numbers" lesson 2.        Check if 3===3.0. Why? 3.        Find the number of zeros such that 3===3.000…01. Why can this happen? 4.    If var x=123e15, and var y=123e-15, does x===x+y? why? 5.    What is typeof NaN? Comment on its (il)logicality. "Number Methods" lesson 6.    What is the hexadecimal number ff (written in JavaScript as 0xff) in base 10? 7.    Write all the numbers from 0 to 20 in hexadecimal form....
1. Coding Assignment the program will start the menu calling the method extractLargestAndSmallestYourName(), which is specified...
1. Coding Assignment the program will start the menu calling the method extractLargestAndSmallestYourName(), which is specified as below.  The method extractLargestAndSmallestYourName() will receive 2 array of integers as arguments, which are denoted as array number 1 and array number 2.  The method will then search for the largest value and the smallest value. The search will also track from which array(s) these values can be found; and  The information will be return an array, which has the...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to...
Write a method that concatenates two strings passed as parameters.    Version #1 - assign to the 1st parameter the concatenation of both parameters. Perform the concatenation using the + operator. We are assuming reference parameter work that way (they don't).    Version #2 - fine. return the value of the 1st param after concatenating to it the second parameter then.    Version #3 - Let's do it the proper way now, I say. Create a temporary String reference variable...
1. There are two steps for evaluating argument, the first step is to determine the logical...
1. There are two steps for evaluating argument, the first step is to determine the logical strength of the argument and the second step is to determine the truth value of the premises. Please discuss whether this 2-step evaluation is good. 2. Informal fallacy can be divided into 4 classes: inconsistence, irrelevancy, insufficiency and inappropriate presupposition. It is called "4I" classification. Discuss whether it Is a good classification.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT