Given a string and an int n, return a string made of the first and last n chars from the string. The string length will be at least n.
public String nTwice(String str, int n) {
public String nTwice(String str, int n) {
return (str.substring(0, n) + str.substring(str.length()-n));
}
Consider the following method.
public static int mystery(int[] arr)
{
int x = 0
for (int k = 0; k < arr.length; k = k + 2)
x = x + arr[k]
return x;
}
Assume that the array nums has been declared and initialized as follows.
int[] nums = {3, 6, 1, 0, 1, 4, 2};
What does this method return?
7
What is my favorite color?
Write a method that returns my favorite color.
public String bennysColor() {
public String bennysColor()
{
return "blue";
}
Given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.
public int[] front11(int[] a, int[] b) {
public int[] front11(int[] a, int[] b)
{
int[] front;
if(a.length >= 1)
{
if(b.length >= 1)
{
front = new int[2];
front[0] = a[0];
front[1] = b[0];
}
else
{
front = new int[1];
front[0] = a[0];
}
}
else if(b.length >= 1)
{
front = new int[1];
front[0] = b[0];
}
else
front = new int[0];
return front;
}
public class SomeClass
{
private int myA;
private int myB;
private int myC;
// Constructor(s) not shown
public int getA
{ return myA; }
public void setB(int value)
{ myB = value; }
The following declaration appears in another class.
SomeClass obj = new SomeClass();
Which of the following code segments will compile without error?
(A) int x = obj.getA();
(B) int x;
obj.getA(x);
(C) int x = obj.myA
(D) int x = SomeClass.getA();
(E) int x = getA(obj);
(A) int x = obj.getA
Consider the following code segment.
int[] arr = {1, 2, 3, 4, 5, 6, 7};
for (int k = 3; k < arr.length - 1; k++)
arr[k] = arr[k + 1]
Which of the following represents the contents of arr as a result of executing the code segment?
(A) {1, 2, 3, 4, 5, 6, 7}
(B) {1, 2, 3, 5, 6, 7}
(C) {1, 2, 3, 5, 6, 7, 7}
(D) {1, 2, 3, 5, 6, 7, 8}
(E) {2, 3, 4, 5, 6, 7, 7}
(C) {1, 2, 3, 5, 6, 7, 7}
Given two ints, each in the range 10..99, return true if there is a digit that appears in both numbers, such as the 2 in 12 and 23. (Note: division, e.g. n/10, gives the left digit while the % "mod" n%10 gives the right digit.)
public boolean shareDigit(int a, int b) {
public boolean shareDigit(int a, int b) {
if(a % 10 == b / 10 || a / 10 == b % 10 || a % 10 == b % 10 || a / 10 == b / 10)
return true;
return false;
}
public static int mystery(int n)
{
int x = 1;
int y = 1;
// Point A
while (n > 2)
{
x = x + y;
// Point B
y = x - y
n--;
}
//Point C
return x;
}
What value is returned as a result of the call mystery(6) ?
8
What happens when you set a value of an ArrayList of type Double to a double?
Can you set a double variable to a Double?
Start with 2 int arrays, a and b, each length 2. Consider the sum of the values in each array. Return the array which has the largest sum. In event of a tie, return a.
public int[] biggerTwo(int[] a, int[] b) {
public int[] biggerTwo(int[] a, int[] b) {
int sum = a[0]+a[1]-b[0]-b[1];
if(sum >= 0)
return a;
return b;
}
Write a method to reverse an array
public static void reverse(int arr[]) {
public static void reverse(int arr[])
{
int j = 0;
int k = nums.length - 1;
while (j < k)
{
int x = nums[j];
nums[k] = x;
j++
k--;
}
}
Assume myList is an ArrayList that has been correctly constructed and populated with objects.
Which of the following expression produces a valid random index for myList?
(A) (int) ( Math.random() * myList.size() ) - 1
(B) (int) ( Math.random() * myList.size() )
(C) (int) ( Math.random() * myList.size() ) + 1
(D) (int) ( Math.random() * (myListsize() + 1) )
(E) Math.random(myList.size())
(B) (int) ( Math.random() * myList.size() )
Write a method that randomizes the locations of each element of a string and returns the string.
public random(String str) {
public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()public ArrayList mystery()
Given a string, if the string begins with "red" or "blue" return that color string, otherwise return the empty string.
public String seeColor(String str)
}
public String seeColor(String str)
{
int len = str.length();
if(len >= 4)
{
if(str.substring(0, 4).equals("blue"))
return "blue";
else if(str.substring(0, 3).equals("red"))
return "red";
else
return "";
}
else if(len == 3)
{
if(str.substring(0, 3).equals("red"))
return "red";
else
return "";
}
else
return "";
}
Write a method that returns an ArrayList<Integer> with consecutive numbers to a random number 1 - 100
public ArrayList mystery() {
public ArrayList mystery()
{
ArrayList<Integer> arr = new ArrayList<Integer>();
int k = 1;
for(int i = 0; i < (int)(Math.random() * 100 + 1); i++)
{
arr.add(k);
k++;
}
return arr;
Name as many methods from the Math class as you can, and describe what they do.
Write a class called Ben with one integer member variable, including appropriate accessors, mutators, and constructors.
public class Ben
{
private int i;
public Ben()
{
i = 0;
}
public int getI()
{
return i;
}
public void setI(int k)
{
i = k;
}
}