Multiple Choice Questions

Go to question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
38 39 40 41
Categories:


© 2015-2016 Benjamin Nockles. All of the questions found through this page are original work. You may print and share, reproduce, or otherwise use these questions for personal or classroom use, so long as they are used for free.

Question 38

posted Jan 30, 2018, 7:16 AM by Benjamin Nockles   [ updated Mar 22, 2018, 6:43 AM ]

Consider the following code segment

Test.java

public static void play(){
    ArrayList<String> list = new ArrayList<String>();
    list.add("dumplings");
    list.add("pizza");
    list.add(1,"tacos");
    list.add("sashimi");
    list.remove(0);
    list.remove(1);
    list.add(1,"gyros")
    list.set(2, "samosas")
   
}

Which of the following represents the contents of list?

A] tacos, sashimi, samosas
B] tacos, gyros, samosas
C] pizza, gyros, samosas
D] gyros, tacos, samosas
E] tacos, gyros, samosas, sashimi

Question 39

posted Mar 3, 2017, 5:33 AM by Benjamin Nockles   [ updated Mar 22, 2018, 6:43 AM ]

< Previous                                  Topic: ArrayList                                         Next >

Consider the following code segment:
ArrayList<String> things = new ArrayList<String>();
things.add("truck");
things.add("bus");
things.add(1,"car");
things.add(0,"motorcycle");
things.remove(1);
things.set(1,"van");
things.add(0,things.remove(things.size()-1);

What are the contents of the List after the above code is executed?

[A] "van", "motorcycle"
[B] "bus", "motorcycle", "truck", "van"
[C] "bus","motorcycle","van",
[D] "van","bus", "motorcycle"
[E] "bus", "motorcycle", "van", "car"

Question 5

posted Oct 25, 2016, 8:24 AM by Benjamin Nockles   [ updated Oct 28, 2016, 11:04 AM ]

< Previous                         Category: Pass By Value                               Next >

Consider the following code:

Sample Code

public static void main(String[] args){
    String s = "Numbers ";
    appendRange(s)
    int x = 1;
    x = increase(x, 2); 
    int[] nums = new int[3];
    populate(nums); 
    System.out.println(s + x+ Arrays.toString(nums));

}

public static void appendRange(String s){
    s = s + " 1 through ";
}

public static int increase(int x, int y){
    return x + y;
}

public static void populate(int[] arr){
arr = new int[3];
for(int i = 0; i < arr.length; i++){
arr[i] = (i+1);
}
}


Which of the following will be printed after the code executes?
[A] Numbers 3 [0, 0, 0]
[B] Numbers 1 through 3 [1, 2, 3]
[C] Numbers 3 [1, 2, 3]
[D] Numbers 1 through 3 [0, 0, 0]
[E] Numbers 1 [0, 0, 0]

Question 32

posted Mar 28, 2016, 6:46 PM by Benjamin Nockles   [ updated Mar 28, 2016, 6:48 PM ]

< Previous      Category: Standard Algorithms         Next >


The following incomplete search method is intended to return the index of an element in a sorted integer array. If the element is not contained in the array, the method returns '-1':

Code Segment

public static int search(int[] nums, int searchValue

{

int low =0;

int high = nums.length -1;

int mid = (low + high)/2;

/* missing code */

{

if(nums[mid] < searchValue){

low = mid + 1;

}else{

high = mid - 1;

}

mid = (low + high)/2;

}

if(low > high) return -1;

return mid;

}


Which of the following can be used to replace /* missing code */ so that search will work as intended?

[A] while (low <= high && nums[mid] == searchValue)
[B] while (low <= high || nums[mid] != searchValue)
[C] while (low <= high && nums[mid] != searchValue)
[D] while (low >= high && nums[mid] == searchValue)
[E] while (low >= high && nums[mid] != searchValue)

Question 31

posted Mar 28, 2016, 6:37 PM by Benjamin Nockles   [ updated Mar 28, 2016, 6:46 PM ]

< Previous                 Category: Standard Algorithms                          Next >

The following incomplete method is intended to return the index of an element in a sorted integer array. If the element is not contained in the array, the method returns '-1':

Code Segment

public static int search(int[] nums, int start, int finish, int target

{

if(start > finish) return -1;

else

{

int mid = (start+finish)/2;

if (nums[mid]==target) return mid;

/* missing code */

{

return search(nums, start, mid-1, target);

}

else return search(nums, mid+1,finish, target);

}

}



Which of the following can be used to replace /* missing code */ so that search will work as intended?
[A] else if (nums[mid] != target)
[B] else if (mid < target)
[C] else if (mid > target)
[D] else if (nums[mid] > target)
[E] else if (nums[mid] < target)

Question 30

posted Mar 28, 2016, 6:16 PM by Benjamin Nockles   [ updated Mar 28, 2016, 6:37 PM ]

< Previous            Category: Standard Algorithms               Next >
Consider the following method:

Code Segment

public int method(int[] nums, int i, int j, int target) {

System.out.println("i = "+i+", j = " + j);

if(i > j) return -1;

else{

int mid = (i+j)/2;

if (nums[mid]==target) return mid;

else if (nums[mid] > target) return method(nums, i, mid-1, target);

else return method(nums, mid+1,j, target);

}

}



If the array nums contains the values [-3, 2, 3, 4, 5, 8, 10, 12, 15, 18], what will print when the call mystery(nums, 0, nums.length-1, 4) is made?

[A] i = 0, j = 9

   i = 0, j = 3

   i = 2, j = 3

[B]  i = 0, j = 9

   i = 0, j = 4

   i = 2, j = 4

[C]   i = 0, j = 9

   i = 0, j = 4

   i = 2, j = 4

   i = 3, j = 4

[D] i = 0, j = 9

   i = 0, j = 3

   i = 2, j = 3

   i = 3, j = 3

[E]   i = 0, j = 9

   i = 0, j = 4

   i = 2, j = 4

   i = 2, j = 3

   i = 3, j = 3

Question 29

posted Mar 7, 2016, 8:39 AM by Benjamin Nockles   [ updated Mar 28, 2016, 6:17 PM ]

< Previous              Category: Reading Errors               Next >
The following method is intended to identify whether two integer arrays contain the same elements, ignoring order. The method does not work. In which line lies the error?

 1     public static boolean containSame(int[] one, int[] two){
 2         if(one.length != two.length) return false;
 3         for(int i=0; i< one.length; i++ ){
 4             boolean contains = false;
 5             for(int j = 0; j < two.length; j++ ){
 6                if(one[i] == two[j] )contains = true;
 7                break;
 8            }
 9            if(!contains) return false;
10         }
11         return true;
12     }



A. Line 2
B. Line 4
C. Line 5
D. Line 6-7
E. Line 9

Question 28

posted Nov 20, 2015, 9:03 AM by Benjamin Nockles   [ updated Sep 28, 2016, 11:55 AM ]

< Previous              Category: Strings               Next >
Consider the following code:

Sample Code

public static String doSomething(String s, String t, String r){
    
    while(s.indexOf(t) > -1){
        int x = s.indexOf(r)+r.length();
        if(x<r.length()) x = s.length();
        s = s.substring(s.indexOf(t)+t.length(), x);
    }
    return s;
}
Which of the following is returned when doSomething("I left to the left while she left to the right, waving goodbye.","left","right")?
    [A]  to the left while she left to the right
    [B]   to the right
    [C]   to the 
    [D]   to the right, waving goodbye.
    [E]  left to the right

Question 27

posted Nov 20, 2015, 8:41 AM by Benjamin Nockles   [ updated Nov 20, 2015, 9:15 AM ]

< Previous         Category: Strings             Next >
Consider the following recursive method:

Method

    public static String mystery(String s, String r){
    int x = s.indexOf(r);
    if(x>-1){
        String t = s.substring(0,x);
        if(x+r.length()<s.length()) t+=s.substring(x+r.length(),s.length());
        return mystery(t,r);
    }else return s;
    }   
What will be returned if mystery("Banana Bandana","an")?
    [A] "Ba Bda"
    [B] "B Bd"
    [C] "Bnna Bndna"
  [D] "Baaa Badaa"
  [E] "Banana Bandana"

Question 26

posted Nov 20, 2015, 8:17 AM by Benjamin Nockles   [ updated Mar 22, 2018, 6:26 AM ]

Consider the following incomplete method:

Incomplete Method

public String getDayOfWeek(int currentDay, int elapsedDays){
    String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    //code not shown;
}
Which of the following would replace //code not shown to return the day of the week, elapsedDays after the currentDay?  (For example getDayOfWeek(3, 2) should return "Friday".  getDayOfWeek(1, 7) should return "Monday".)
[A] return days[(currentDay+elapsedDays)/days.length];
[B] return days[elapsedDays%days.length];
[C] return days[(currentDay+elapsedDays+1)%days.length];
[D] return days[(currentDay+elapsedDays+1)/days.length];
[E] return days[(currentDay+elapsedDays)%days.length];

1-10 of 30