46: What does the following code snippet display? char ch1 = \'\\ u0000\'; char
ID: 3687712 • Letter: 4
Question
46:
What does the following code snippet display?
char ch1 = ' u0000';
char ch2 = ' uffff';
for (int i = 0; i < 1000; i++)
{
if (i % 50 == 0)
{
System.out.println();
}
System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 +
1)));
}
Select one:
a. It displays random Unicode characters.
b. It displays random ASCII characters.
c. Nothing because it has compilation error.
d. It displays the hexadecimal characters between '0' and 'F'
47:
Given the following method, what method call will return true?
public static boolean isValid(String input)
{
boolean valid = true;
if (input.length() != 11)
{
valid = false;
}
else
{
if (input.charAt(3) != '-' || input.charAt(6) != '-')
{
valid = false;
}
else
{
valid =
Character.isDigit(input.charAt(0)) &&
Character.isDigit(input.charAt(1)) &&
Character.isDigit(input.charAt(2)) &&
Character.isDigit(input.charAt(4)) &&
Character.isDigit(input.charAt(5)) &&
Character.isDigit(input.charAt(7)) &&
Character.isDigit(input.charAt(8)) &&
Character.isDigit(input.charAt(9)) &&
Character.isDigit(input.charAt(10));
}
}
return valid;
}
Select one:
a. isValid("123-45-67")
b. isValid("123-456789")
c. isValid("123-45-6789")
d. isValid("ABC-45-6789
48.
1.00
Evaluate the pseudocode below to calculate the payment (pmt) with the following
test values:
The total number of hours worked (workingHours) = 60
The rate paid for hourly work (rate) = 15
Input workingHours
Input rate
pmt = workingHours * rate
If workingHours > 40 then
extraHours = workingHours – 40
extraPmt = extraHours * rate * 2
pmt = pmt + extraPmt
Output pmt
Select one:
a. 900
b. 1,200
c. 1,500
d. 1,800
49.
What are the values of num1 and num2 after this snippet executes?
double num1 = 4.20;
double num2 = num1 * 10 + 5.0;
Select one:
a. num1 = 4.20 and num2 = 42.0
b. num1 = 4.20 and num2 = 47.0
c. num1 = 42.0 and num2 = 42.0
d. num1 = 42.0 and num2 = 47.0
50.
What are the values of i and j after the following code snippet executes?
int i = 60;
int j = 50;
int count = 0;
while (count < 5)
{
i = i + i;
i = i + 1;
j = j - 1;
j = j - j;
count++;
}
System.out.println(i);
System.out.println(j);
Select one:
a. i = 65, j = 1
b. i = 65, j = 45
c. i = 1951, j = 0
d. i = 1951, j = 45
51.
Consider the following code snippet. What does the array contain at the end of the
program?
public static fillWithRandomNumbers(double[] values)
{
double[] numbers = new double[values.length];
for (int i = 0; i < numbers.length; i++)
{
numbers[i] = Math.random();
}
values = numbers;
}
public static void main(String[] args)
{
double[] num = new double[20];
fillWithRandomNumbers(num);
}
Select one:
a. 20 random numbers
b. Undefined data due to compilation error
c. 20 zeros because array num is not changed by method
d. Array index bound error
52:
A store applies a 15 percent service charge on all items with a price of at least
$150. No service charge is otherwise applicable. Which of the following DOES NOT
correctly compute the service charge?
Select one:
a.
double serviceCharge = 0;
if (cost >= 150)
{
serviceCharge = 0.15 * cost;
}
b.
double serviceCharge = 0.15 * cost;
if (cost <= 150)
{
serviceCharge = 0;
}
c.
double serviceCharge;
if (cost < 150)
{
serviceCharge = 0;
}
else
{
serviceCharge = 0.15 * cost;
}
d.
double serviceCharge = 15;
if (cost >= 150)
{
serviceCharge = 0.15 * cost;
}
else
{
serviceCharge = 0;
}
53:
Consider the following pseudocode, what does it produce?
Set n = 0.
Set a = 0.
Set b = 1.
Set p = 1.
Print p.
Repeat until n equals 10
Set p = a + b.
Set a = b.
Set b = p.
Add 1 to n.
Print p.
Select one:
a. 1 3 5 7 9 11 13 15 17 19 21
b. 1 1 2 3 5 8 13 21 34 55 89
c. 1 2 3 4 5 6 7 8 9 10 11
d. 1 3 6 9 12 15 18 21 24 27 30
54:
Consider the following Java variable names:
I. 1stInstance
II. basicInt%
III. empName_
IV. addressLine1
V. DISCOUNT
Which of the following options is correct?
Select one:
a. Only IV is a valid Java variable name.
b. Only I and IV are valid Java variable names.
c. Only I, IV, and V are valid Java variable names.
d. Only III, IV, and V are valid Java variable names
55.
Given the following code snippet, what should we change to have 26 alphabet
characters in the string str?
String str = "";
for (char c = 'A'; c < 'Z'; c++)
{
str = str + c;
}
Select one:
a. int c = 'A'
b. str = c + str;
c. c <= 'Z'
d. Must change to use a do loop
56:
Given the following code, which argument(s) will cause the method to return true?
publicstaticboolean isIdeal(String s)
{
int low = 0;
int high = s.length() - 1;
while (low < high)
{
if (s.charAt(low) != s.charAt(high))
{
returnfalse;
}
low++;
high--;
}
returntrue;
}
I. isIdeal("civic")
II. isIdeal("level")
III. isIdeal("race car")
IV. isIdeal("rotor")
Select one:
a. I only
b. I and II only
c. I, II, and III
d. I, II, and IV
57:
Because Java was designed for the Internet, which two of its attributes make it
suitable for beginning programmers?
Select one:
a. Efficiency and portability
b. Safety and security
c. Safety and portability
d. Efficiency and security
58:
Given the code below, what is the output of the method call hashIt("#", 6)?
public static void hashIt(String s, int d)
{
if (d <= 1)
{
System.out.print(d);
}
else
{
s = s + "/";
hashIt(s, d – 2);
System.out.print(s + d);
}
}
Select one:
a. 0#///2#//4#/6
b. 0#/2#//4#///6
c. 0/2#//4#///6#
d. #/6#//4#///20
59.
Assuming that a user enters 10, 20, and 30 as input values one after another,
separated by spaces, what is the output of the following code snippet?
int num1 = 0;
int num2 = 0;
int num3 = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
num1 = in.nextInt();
System.out.print("Enter a number: ");
num2 = in.nextInt();
System.out.print("Enter a number: ");
num3 = in.nextInt();
if (num1 > num2)
{
if (num1 > num3)
{
System.out.println(num1);
}
else
{
System.out.println(num3);
}
}
else
{
if (num2 > num3)
{
System.out.println(num2);
}
else
{
System.out.println(num3);
}
}
Select one:
a. 0
b. 10
c. 20
d. 30
60.
How many times does the code snippet given below display "Loop Execution"?
int i = 1;
while (i != 10)
{
System.out.println ("Loop Execution");
i++;
}
Select one:
a. Infinite times
b. 8 times
c. 9 times
d. 10 times
Explanation / Answer
46)
c. Nothing because it has compilation error.
There are errors in the assignment of values to the character variables. Space should not be there after slash in assignment.
47)
c. isValid("123-45-6789")
The string 123-45-6789 is a valid string. Because, the length is 11, character at 3rd and 6th positions is ‘-’ and there exists digits at positions 0,1,2,4,5,7,8,9,10.
48)
c. 1,500
workingHours = 60
rate = 15
pmt = 60 *15
= 900
extraHours = 20
extrapmt = 20*15*2
= 600
pmt = 900+600
= 1500
49)
b. num1 = 4.20 and num2 = 47.0
num1 = 4.20
num2 = 4.20*10+5.0
= 47.0
50)
c. i = 1951, j = 0
In the first iteration, i=121 and j=0.
In the second iteration, i=243 and j=0.
In the third iteration, i=487 and j=0.
In the fourth iteration, i=975 and j=0.
In the fifth iteration, i=1951 and j=0.
51)
b. Undefined data due to compilation error
In the function, there is no return type specified. It results in compilation error.
52)
b.
double serviceCharge = 0.15 * cost;
if (cost <= 150)
{
serviceCharge = 0;
}
In the above code, if condition fails for cost equals to 150. Service charge should be there for cost=150.
53)
b. 1 1 2 3 5 8 13 21 34 55 89
54)
d. Only III, IV, and V are valid Java variable names
The variable names III, IV and V satisfies the java naming rules.
55)
c. c <= 'Z'
If the condition changed to c <= 'Z', all the letters will be printed.
56)
d. I, II, and IV
57)
c. Safety and portability
58)
a. 0#///2#//4#/6
59)
d. 30
60)
c. 9 times
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.