Test
Programming
Este test tiene tipos de pregunta que se abren en el reproductor de siempre.
Preguntas del test (200)
1In Java, the same program can be run on different platforms (Windows, Macs, Linux, etc) without changing the code.
- AVerdadero
- BFalso
2The Java Virtual Machine takes the Java source code and translates it to byte code.
- AVerdadero
- BFalso
3Each platform that runs a Java program needs its own Java Virtual Machine (JVM), to translate byte code to that platform's machine code.
- AVerdadero
- BFalso
4In Pair Programming, a pair of programmers sit next to each other, and each person types using their own computer.
- AVerdadero
- BFalso
5In Pair Programming, one person is the navigator while the other person types the code.
- AVerdadero
- BFalso
6Enter your answer in all lowercase letters.
The physical computer and peripheral devices of a computer are collectively called [blank_start][answer][blank_end]- Ahardware
7A compiler translates high-level instructions into more detailed instructions called [blank_start][answer][blank_end] code.
- Amachine
8Java contains a vast set of [blank_start][answer][blank_end] packages that are required to write useful programs.
- Alibrary
9We use [blank_start][answer][blank_end] as our integrated development environment (IDE) to write and test Java programs.
(Capitalize the first letter of the name of the IDE)- ANetbeans
10A [blank_start][answer][blank_end] is a collection of programming instructions that describe how to carry out a particular task.
- Amethod
11Every Java application must have a [blank_start][answer][blank_end] method which contains the statements that are executed one by one.
- Amain
12A sequence of characters enclosed in quotation marks is called a [blank_start][answer][blank_end]. "Hello World!" is an example of this.
- Astring
13Every Java statement must end in a [blank_start][answer][blank_end], otherwise the compiles gets confused as to when a statement ends and another one begins.
- Asemicolon
14When something is wrong in your program according to the rules of the language, you get a [blank_start][answer][blank_end]-time error, also known as syntax errors.
- Acompile
15When the program is syntactically correct, but does something it isn't supposed to do, you get a [blank_start][answer][blank_end]-time error, which is sometimes caused by a logic error.
- Arun
16A "division by zero" is an example of a [blank_start][answer][blank_end]-time error.
- Arun
17MIsspelling words is an example of a [blank_start][answer][blank_end] error.
- Asyntax
18Organizing instructions in the wrong order is an example of a [blank_start][answer][blank_end] error.
- Alogic
19A sequence of steps that is unambiguous, executable, and terminating is called an [blank_start][answer][blank_end].
- Aalgorithm
20The steps that are read by a HUMAN (not a computer) to describe how a value is set or changed, or to describe the decisions and repetitions needed to solve a problem, are generally called [blank_start][answer][blank_end].
- Apseudocode
21There are usually 4 steps to writing an algorithm using pseudocode. Fill in the blanks below:
Step 1: Determine the [blank_start][answer][blank_end] and the outputs.
Step 2: Break down the problem into smaller [blank_start][answer][blank_end].
Step 3: Describe each [blank_start][answer][blank_end] in pseudocode.
Step 4: [blank_start][answer][blank_end] your pseudocode by working on a problem.- Ainputs
- Btasks
- Csubtask
- DTest
22The [blank_start][answer][blank_end] in a computer performs program control and data processing.
- ACPU
23There are at least 2 types of data storage devices:
[blank_start][answer][blank_end] and secondary storage.- Amemory
24Java was originally designed by James Gosling to program [blank_start][answer][blank_end] devices.
- Aconsumer
25Java programs are distributed as instructions for a [blank_start][answer][blank_end] machine.
- Avirtual
26One of the benefits of Java is that it is [blank_start][answer][blank_end]-independent.
- Aplatform
27The Java compiler translates source code into class files, also called [blank_start][answer][blank_end] code.
- Abyte
28The fundamental building blocks of Java programs are [blank_start][answer][blank_end].
- Aclasses
29Every Java application contains a class with a [blank_start][answer][blank_end] method, so that when the application starts, the instructions in that method are executed first.
- Amain
30Each class contains blocks of code that contain a sequence of instructions. These blocks of code are called [blank_start][answer][blank_end].
- Amethods
31When a method needs to be passed values to do its task, those values are called [blank_start][answer][blank_end]
- Aarguments
32[blank_start][answer][blank_end] is the act of designing and implementing computer programs.
- AProgramming
33Java is [blank_start][answer][blank_end] sensitive, so you must be careful about distinguishing between upper and lowercase letters.
- Acase
34There are 2 main benefits of Java:
1. safety
2. [blank_start][answer][blank_end]- Aportability
35Because Java contains a vast set of [blank_start][answer][blank_end] packages, even expert Java programmers only use the packages that they need for particular projects. There are packages for graphics, user-interface design, cryptography, networking, database storage, etc.
- Alibrary
36A comment is text added to code by a programmer, compiled into machine code.
- AVerdadero
- BFalso
37Single-line comments begin with /*
- AVerdadero
- BFalso
38A memory is a circuit that can store 0s and 1s in each of a series of thousands of addressed locations.
- AVerdadero
- BFalso
39The Java Virtual Machine interprets the .class file (byte code), allowing the same code to be portable to different processor types without having to re-compile.
- AVerdadero
- BFalso
40A statement like the one below causes the compiler to allocate a memory location, ready to store a value.
int userAge;- AVerdadero
- BFalso
41It is not a good practice to initialize a variable at the same time as its definition.
- AVerdadero
- BFalso
42Multiple variables can not be defined in the same statement, so the following statement is illegal:
int numProtons, numNeutrons, numElectrons;.- AVerdadero
- BFalso
43After executing the following sequence of Java statements, the variable, avgKidsPerFamily, will have a value of 3.5
int kidsInFamily1 = 3;
int kidsInFamily2 = 4;
int numFamilies = 2;
double avgKidsPerFamily = 0.0;
avgKidsPerFamily = (kidsInFamily1 + kidsInFamily2) / numFamilies;- AVerdadero
- BFalso
44Given that myIntVar = 7, then (double)myIntVar converts int 7 to double 7.0. This is called "casting."
- AVerdadero
- BFalso
45 keyboard.next() is used to get the next string from user input, keyboard.[blank_start][answer][blank_end] is the method used to get the next integer.
- AnextInt()
46[blank_start][answer][blank_end] is used for integers expected to exceed about 2 billion.
- ALong
47What is the decimal value for the following binary number?
0010 1101- A43
- B44
- C45
- D46
48What is the binary representation for the following decimal number?
25- A0001 1000
- B0001 1001
- C0001 1010
- D0001 1011
49When using the Scanner class for input, the object that will read input from the keyboard is System.in
- AVerdadero
- BFalso
50In order to use the Scanner class, you must import the java.util.JOptionPane class.
- AVerdadero
- BFalso
51The JOptionPane.showInputDialog() method is used to display a message to the user AND to provide a textbox for the user to input data.
- AVerdadero
- BFalso
52The JOptionPane.showMessageDialog() method is used only to get input from the user.
- AVerdadero
- BFalso
53To be able to use JOptionPane, you must import javax.swing.JOptionPane.
- AVerdadero
- BFalso
54In a class definition, the purpose of a constructor is to initialize all the values of the attributes defined in that class (instance variables).
- AVerdadero
- BFalso
55The name of the constructor does not have to be the same as the name of the class.
- AVerdadero
- BFalso
56A default constructor is provided by Java automatically, whenever the programmer does not explicitly code a constructor.
- AVerdadero
- BFalso
57A class constructor can NEVER be overloaded.
- AVerdadero
- BFalso
58The parameter order and data type passed to a constructor must match exactly the order of the arguments passed in the call to the constructor.
- AVerdadero
- BFalso
59All methods require a name, an access type (public, private), and a return type (e.g. int, double, String).
- AVerdadero
- BFalso
60In the calculateMPG( ) method from the video, 2 int parameters were defined as input to the method, and 1 int value was returned from the method.
- AVerdadero
- BFalso
61A method that returns a value can either be assigned to a variable or it can be embedded inside a System.out.println() method.
- AVerdadero
- BFalso
62Decide the best answer for the following code:
int x = 6; int y = 5; int z = 10;
if(x < z || z < y)
{
System.out.println(“True.”);
}
else
{
System.out.println(“False.”);
}- A“False” will be printed, because both conditions need to be true in order to print “True.”
- BBoth conditions are true therefore “True” will be printed.
- CSince at least one of the conditions is true, “True” will be printed.
- DSince at least one of the conditions is false, “False” will be printed.
63Decide the best answer for the following code:
char firstGL = 'A';
int studentYear = 4;
if(studentYear >= 4)
{
if(firstGL != 'A')
{
System.out.println(" Sorry! You have to take another GL course.");
}
else
{
System.out.println( “Congratulations! You do not have to take another GL course.");
}
} else
System.out.println("You need to be at least a 4th year student to be exempted.");- AThe student will have to take a second GL course because the student has to be in a higher year.
- BThe student does not have to take a second GL course because they have met all of the requirements for an exemption.
- CA grade of “A” is not necessary in order to qualify for an exemption. Being a 4th year student is sufficient for qualification.
- DBoth b and c.
64Decide the best answer for the following code:
char grade = ‘B’;
switch (grade)
{
case ‘A’:
System.out.println(“GPA is 4.0”);
break;
case ‘B’:
System.out.println(“GPA is 3.0”);
break;
case ‘C’:
System.out.println(“GPA is 2.0”);
break;
case ‘D’:
System.out.println(“GPA is 1.0”);
break;
default:
System.out.println(“No other options.”);
break;
}- ABreaks are always optional and at the discretion of the programmer and doesn’t make a difference.
- BIf the break at case ‘D’ were to be removed it would not affect the logic of the code whatsoever.
- CIf the break at case ‘B’ were to be removed case ‘C’ will become the only answer.
- DThe code above follows “if else logic”.
65Choose the correct format for the switch:
- Aa. switch (numeric value)
{
case one:
code to be executed;
break;
case two:
code to be executed;
break;
default:
code to be executed;
break;
} - Bswitch (variable == variable)
{
case 1:
code to be executed;
break;
case two:
code to be executed;
break;
else:
code to be executed;
break;
} - Cswitch (variable)
{
case 1:
code to be executed;
break;
default:
code to be executed;
break;
} - Dnone of these.
- Aa. switch (numeric value)
66
for(int value = 0; value < 2; value = value + 1)
{
System.out.println(value)
}
What logical tests does the for- loop perform before ending?- A0 < 2
1 < 2
2 < 2 - B1 < 2
2 < 2 - C0 < 2
1 < 2 - D0 < 2
- A0 < 2
67int sum = 0;
for(int i = 5; i > 0; i = i - 1)
{
sum = sum + i;
}
What is the value of “i” BEFORE the third iteration?- A5
- B4
- C2
- D3
68int sum = 0;
for(int i = 5; i > 0; i = i - 1)
{
sum = sum + i;
}
What is the value of “i” AFTER the third iteration?- A5
- B4
- C2
- D3
69int sum = 0;
for(int i = 5; i > 0; i = i - 1)
{
sum = sum + i;
}
What is the value of “sum” AFTER the third iteration?- A5
- B9
- C12
- D14
70int i = 5;
for(i = 0; i < 2; i = i + 1)
{
System.out.println(i)
}
What is the first value of “i” to be printed?- A5
- B0
- C1
- D2
71A class and an object are the same thing.
- AVerdadero
- BFalso
72A class is like a blueprint for a house, where as an object is like the actual house built from the blueprint.
- AVerdadero
- BFalso
73A class is defined by the attributes and methods that it has.
- AVerdadero
- BFalso
74A getter method returns the value in an attribute.
- AVerdadero
- BFalso
75A setter method returns the value in an attribute.
- AVerdadero
- BFalso
76The result of evaluating the condition of an if-statement...is always a boolean value.
- AVerdadero
- BFalso
77A switch statement can do everything that an if-statement can do
- AVerdadero
- BFalso
78There are 4 types of if-statements:
1.) if-else
2.) if- else if
3.) nested if's
4.) compound if-statements,joined by && or ||- AVerdadero
- BFalso
79A short-circuit logical operator like && can be used to prevent an abnormal termination like the one produced when dividing by zero.
- AVerdadero
- BFalso
80If 2 conditions needs to be true, you can use a compound if-statement joined by &&.
- AVerdadero
- BFalso
81A nested-if statement should be used when you have overlapping conditions being tested. For example, if you are testing if the user can drive, and if so, can he/she also vote, and if so, can he/she also drink.
- AVerdadero
- BFalso
82The following is an example of a valid expression to compare 2 numbers:
if (a = b)
System.out.println("They are equal");
else
System.out.println("They are not equal");- AVerdadero
- BFalso
83When age is set to 15, only one message below will print.
if (age < 21)
System.out.println("You can not drink legally yet");
if (age < 18)
System.out.println("You can not vote yet");
if (age < 16)
System.out.println("You can not drop out of school legally");- AVerdadero
- BFalso
84When the value of userNum is set to be 87, only 1 statement below will be printed:
if (userNum < 60)
System.out.println("You failed");
else if (userNum < 70)
System.out.println("D");
else if (userNum < 80)
System.out.println("C");
else if (userNum < 90)
System.out.println("B");
else if (userNum <= 100)
System.out.prinltn("A");- AVerdadero
- BFalso
85When the value of userNum is set to be 75, only 1 statement below will be printed:
if (userNum < 60)
System.out.println("You failed");
if (userNum < 70)
System.out.println("D");
if (userNum < 80)
System.out.println("C");
if (userNum < 90)
System.out.println("B");
if (userNum <= 100)
System.out.prinltn("A");- AVerdadero
- BFalso
86What number will be inside of numBoxes as a result of the if-statements below?
numBoxes = 0; numApples = 9;
if (numApples < 10)
{ if (numApples < 5)
{ numBoxes = 1; }
else
{ numBoxes = 2; }
}
else if (numApples < 20)
{
numBoxes = numBoxes + 1;
}
[blank_start]_______[blank_end]- A2
87If 2 conditions must be true, you can use a compound if-statement with an && to check for both conditions.
- AVerdadero
- BFalso
88Another way to test if the user entered the "y" for an answer is to use the following switch statement:
switch (userAnswer)
{
case "Y":
case "y":
System.out.println("You entered Y/y to continue");
break;
default:
System.out.println("You entered an invalid answer.");
}- AVerdadero
- BFalso
89A switch statement can do everything that an if-statement can do
- AVerdadero
- BFalso
90When the value of userNum is set to be 75, only 1 statement below will be printed:
if (userNum < 60)
System.out.println("You failed");
if (userNum < 70)
System.out.println("D");
if (userNum < 80)
System.out.println("C");
if (userNum < 90)
System.out.println("B");
if (userNum <= 100)
System.out.prinltn("A");- AVerdadero
- BFalso
91When the value of userNum is set to be 87, only 1 statement below will be printed:
if (userNum < 60)
System.out.println("You failed");
else if (userNum < 70)
System.out.println("D");
else if (userNum < 80)
System.out.println("C");
else if (userNum < 90)
System.out.println("B");
else if (userNum <= 100)
System.out.prinltn("A");- AVerdadero
- BFalso
92How many times will the following loop run?
int i = 0;
while (i < 10)
{
System.out.println(i);
i++;
}- A9
- B10
- C8
- D0
93What is the output of the code below?
for (int val = 0; val < 4; val ++)
{
System.out.print("+");
for (int num = 0; num < val; num++)
{
System.out.print("0");
}
}- A++++000000
- B+0+00+000+0000
- C++0++00+000
- D+000+000+000+000
94What is the output of the following code fragment?
int i = 1;
int sum = 0;
while (i <+ 11)
{
sum = sum + i;
i++;
}
System.out.println("The value of sum is " + sum);
}- AThe value of sum is 56.
- BThe value of sum is 65.
- CThe value of sum is 55.
- DThe value of sum is 66.
95What does the method below return?
public static int findSomething (String str)
{
int position = 0;
while (position < str.length() && (str.charAt(position) != 'e'))
{
position++;
}
return position;
}- Athe position of the last 'e' in the string or the length of the string if there is no 'e'
- Bthe position of the first 'e' in the string of the length of the string if there is no 'e'
- Cthe position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
- Dthe position of the last character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
96How many times will the output line be printed in the following code snippet?
for (int num2 = 1; num2 <=3; num2 ++)
{
for (int num1 = 0; num1 < 2; num1++)
{
System.out.println("" + num2 + " " + num1);
}
}- A9 times
- B12 times
- C3 times
- D6 times
97What is the output of the following code snippet?
int 1 = 1;
while (i <= 10)
{
System.out.println ("Inside the while loop");
i = i + 10;
}- ANo output after successful compilation.
- B"Inside the while loop" will be displayed 10 times.
- C"Inside the while loop" will be displayed only once.
- DNo output because of compilation error.
98What does the following code do?
int sum = 0;
final double count = 1000;
Random generator = new Random();
for (int i = 1; i <= count; i++)
{
sum = sum + generator.nextInt(101);
}
System.out.println(sum / count);- AIt simulates the outcome of throwing a coin.
- BIt performs a Monte Carlo simulation to estimate the value of pi.
- CIt calculates the average of 1000 random numbers between 0 and 100.
- DIt calculates the average of 1000 random numbers between 1 and 101.
99Assume the following variable has been declared and given a value as shown:
Random rand = new Random();
Which of the following will generate a random integer in the range - 20 to 20, inclusive, where each value has an equal chance of being generated?- Arand.nextInt (20) - 41
- Brand.nextInt (-20, 20)
- Crand.nextInt (41) - 20
- Drand.nextInt (-20) + 40
100Which of the following code snippets displays the output exactly 10 times?
- Aint i = 0;
while (i < 10);
{
System.out.println("This is example 1.");
i++;
} - Bint i = 0;
while (i < 10)
{
System.out.println("This is example 3.");
} - Cint i = 1;
while (i < 10)
{
System.out.println("This is example 4.");
i++;
} - Dint i = 0;
while (i < 10)
{
System.out.println("This is example 2.");
i++;
}
- Aint i = 0;
101What for loop can be used in the indicated area so the code will print"
****
***
**
*
for (int val = 0; val < 4; val ++)
{
//Put for loop here
{
System.out.print("*");
}
System.out.println();
}- Afor (int num = 0; num < val; num++)
- Bfor (int num = 4; num < val; num++)
- Cfor (int num = 4; num > 0l; num--)
- Dfor (int num = 0; num < 4-val; num++)
102All it takes for an OR expression to be true is for one of the subexpressions to be true.
- AVerdadero
- BFalso
103If the expression on the left side of the && operator is false, the expression on the right side will not be checked.
- AVerdadero
- BFalso
104If str1 and str2 are both String objects, which of the following expressions will correctly determine whether or not they are equal?
- Astr1 == str2
- Bstr1 && str2
- Cstr1.equals(str2)
- Dstr1 + str2
105What is the value of ans after the following code has been executed?
int x = 40;
int y = 40;
int ans = 0;
if (x = y)
ans = x + 10;- A30
- B80
- C50
- DThe code contains an error and will not compile.
106What will be displayed when the following code is executed?
double x = 45678.259;
DecimalFormat formatter = new DecimalFormat ("#,###,##0.00");
JOptionPane.showMessageDialog(null,formatter.format(x));- A45,678.26
- B45,678.3
- C0,045,678.26
- D45678.259
107What will be the value of bonus after the following code is executed?
int bonus, sales = 10000;
if (sales < 5000)
bonus = 200;
else if (sales < 7500)
bonus = 500;
else if (sales < 10000
bonus = 750;
else if (sales < 20000)
bonus = 1000;
else
bonus = 1250;- A750
- B1250
- C500
- D1000
108What will be the values of ans, x and y after the following statements are executed?
int ans = 35, x = 50, y = 50;
if (x >= y)
{
ans = x + 10;
x -= y;
}
else
{
ans = y + 10;
y += x;
}- Aans = 60, x = 0, y = 50
- Bans = 45, x = 50, y = 0
- Cans = 45, x = 50, y = 50
- Dans = 60, x = 50, y = 100
109What would be the value of discountRate after the following statements are executed?
double discountRate = 0.0;
int purchase = 1250;
char crust = 'N';
if (purchase > 1000)
if (crust == 'Y')
discountRate = 0.05;
else
discountRate = 0.04;
else if (purchase > 750)
if (crust == 'Y')
discountRate = 0.04;
else
discountRate = 0.03;
else
discountRate = 0.0
- A0.0
- B0.04
- C0.05
- D0.03
110What would be the value of x after the following statements were executed?
in x = 10;
switch (x)
{
case 10:
x +=15;
case 12:
x -= 5;
break;
default:
x *= 3;
}- A30
- B20
- C25
- D5
111Which of the following expressions could be used to perform a case-insensitive comparison of two String objects named str1 and str2?
- Astr1.equalsIgnoreCase(str2)
- Bstr1.equalsInsensitive(str2)
- Cstr1 != str2
- Dstr1 || str2
112Which of the following expressions determines whether the char variable charA is not equal to the letter 'A'?
- AcharA == 'A'
- BcharA != 'A'
- CcharA || 'A'
- DcharA.notequals( 'A' )
113Which of the following statements determines whether temp is within the range of 0 through 100, including 0 & 100?
- Aif (temp >= 0 && temp <= 100)
- Bif (temp >=0 && temp < 100)
- Cif (temp >= 0 || temp <= 100)
- Dif (temp > 0 && temp < 100)
114int value = 1;
while (value < 1)
{
System.out.println(value);
value++;
}
What logical tests does the while loop perform before ending?- A1 < 1 (first iteration)
2 < 1 (second iteration) - B1 < 1
- C2 < 1
- D2 < 1 (first iteration)
1 < 1 (second iteration)
- A1 < 1 (first iteration)
115int value = 1;
do
{
System.out.println(value);
value++;
}while(value < 1);
What logical tests does the do-while loop perform before ending?- A1 < 1 (first iteration)
2 < 1 (second iteration) - B1 < 1
- C2 < 1
- D2 < 1 (first iteration)
1 < 1 (second iteration)
- A1 < 1 (first iteration)
116int sum = 1;
while(sum < 10)
{
sum = sum + (sum * 2);
System.out.print(sum + " ");
}
Which do-while loop outputs the same as the above while loop? Assume the variable "sum" is initialized to 1.- Ado
{
sum = sum + (sum * 2);
System.out.print(sum + " ");
}while(sum < 10); - Bdo
{
sum = sum + (sum * 2);
System.out.print(sum + " ");
}while(sum > 10); - Cdo
{
System.out.print(sum + " ");
sum = sum + (sum * 2);
}while(sum < 10);
- Ado
117int sum = 1;
while(sum < 10)
{
sum = sum + (sum * 2);
System.out.print(sum + " ");
}
What is the value of "sum" AFTER the second iteration of the loop?- A27
- B3
- C1
- D9
118int sum = 1;
while(sum < 10)
{
sum = sum + (sum * 2);
System.out.print(sum + " ");
}
What is the value of "sum" BEFORE the second iteration of the loop?- A27
- B3
- C1
- D9
119Consider the following code segment:
int p = 5;
int q = 2;
int sum = 0;
while (p <= 5)
{
sum += p % q;
p++;
q++;
}
What is the value of sum after the code is executed?- A1
- B0
- C10
- D7
120What are the values of a and b after the for loop finishes?
int a = 5, b = 2, temp;
for (int i=1; i<=4; i++) {
temp = a;
a = i + b;
b = temp – i; }- Aa = 4 and b = 3
- Ba = 7 and b = 0
- Ca = 2 and b = -2
- Da = 5 and b = 2
121A loop is a construct that repeatedly executes specific code as long as some condition is false.
- AVerdadero
- BFalso
122To test a condition in a loop expression, you should use = rather than the equality operator ==
- AVerdadero
- BFalso
123A good practice is to include greater-than or less-than along with equality in a loop expression.
- AVerdadero
- BFalso
124An infinite loop is a loop that will always execute (i.e., execute infinitely) because the loop's expression always evaluates to true
- AVerdadero
- BFalso
125A for loop statement collects three parts—the loop variable initialization, loop expression, and loop variable update.
- AVerdadero
- BFalso
126The following for-loop will execute 9 times:
for (int i = 0; i <= 9; i++)- AVerdadero
- BFalso
127What is 3 * 4 + 2 - 7 % 2 = ?
- A1
- B14
- C13
- D10
128What is the minimum requirement for the following while-loop to end:
while (computerTotal < 21 && humanTotal < 21)
{
....
}- Awhen either computerTotal == 21 OR humanTotal == 21
- Bwhen BOTH computerTotal == 21 AND humanTotal == 21
- Cwhen computerTotal == 15
- Dwhen humanTotal == 15
129In order to check if the user entered "yes" as their answer, we should use the equalsIgnoreCase method of the String class, like this:
if (userAnswer.equalsIgnoreCase("yes")) .....- AVerdadero
- BFalso
130Another way to test if the user entered the "y" for a String answer is to use the following switch statement:
switch (userAnswer)
{
case "Y":
case "y":
System.out.println("You entered Y/y to continue");
break; default:
System.out.println("You entered an invalid answer.");
}- AVerdadero
- BFalso
131Look at the following nested for-loop, and answer what is the purpose of the nested for-loop?
for (int emp = 1; emp <= 5; emp++)
{
hours = 0;
System.out.println("What is the hourly rate of emp# " + emp + "?"); hourlyRate = keyboard.nextDouble();
for (int day = 1; day <= 10; day++) {
System.out.println("How many hours did emp#" + emp + " work on day# " + day + " ? ");
hours += keyboard.nextInt(); }
System.out.println("Emp# " + emp + " earned " + (hours * hourlyRate) + " in the past 10 days.");
}- Ato get a varying amount of hours worked each day
- Bto accumulate the amount of hours worked in a 10-day period
- Cto provide an accumulator that is re-initialized to 0 for each employee
- Dall of the reasons listed
132Explain why the following loop asks for the number of tokens per game:
int numTokens = 100;
int tokensPerGame = 0;
while (numTokens > 0) {
System.out.println("How many tokens are needed to play the next game?"); tokensPerGame = keyboard.nextInt();
if (tokensPerGame < numTokens)
{
numTokens -= tokensPerGame;
System.out.println("You now have " + numTokens + " tokens left to play with."); }
else {
System.out.println("Sorry, you have insu cient tokens to play that game. Pick another one.");
}
}
System.out.println("Thanks for playing!")- Ato calculate the cost of playing each game
- Bto ensure there are enough tokens available to play the selected game
- Cto accumulate the total number of tokens used to play games
- Dto decrease the total number of tokens left by the amount of tokens needed for the selected game
133What does the following loop accomplish?
int aNum;
Random myRan = new Random();
for (int ticket = 1; ticket <=10; ticket++) {
for int lottoNum = 1; lottoNum <= 5; lottoNum++) {
aNum = myRan.nextInt(36) + 1;
System.out.print(" " + aNum + " ");
}
System.out.println("");
}- Agenerates 10 lotto tickets with 5 numbers each
- Bgenerates 5 lotto tickets with 10 numbers each
- Censures each lottery number is between 1 and 35
- Densure each lottery number is between 1 and 36
134String favColor = "blue"; String guessColor = "";
Scanner keyboard = new Scanner (System.in);
do {
System.out.println("Guess what is my favorite color: "); guessColor = keyboard.next();
if (guessColor.equalsIgnoreCase(favColor))
{
System.out.println("You got it!");
}
} while (! guessColor.equalsIgnoreCase(favColor));
}- AfavColor is a String initialized with "blue" but guessColor is a String initialized with ""
- Bthe if-statement in the do-while loop should say if (guessColor.equalsIgnoreCase(favColor))
- Cthe condition of the do-while should be replaced with (! guessColor.equalsIgnoreCase(favColor))
- Dthe if-statement in the do-while loop should say if guessColor != favColor)
135When reading files using the Scanner class and the File class, the Scanner class offers multiple hasNext() methods for various data types such as int, double, String, etc.. For example, the hasNextInt() method returns true if an integer is available for reading, and false if the next item in the file is not an integer or if the end of the file has been reached.
- AVerdadero
- BFalso
136Because of the likelihood that a file fails to open, usually because the file does not exist or is in use by another program, the main() method definition contains a throws clause specifying that an exception of type IOException may be thrown.
- AVerdadero
- BFalso
137When done using the file, the program should leave the file open since there is no need to issue a close() method .
- AVerdadero
- BFalso
138The following statement opens the file:
FileWriter obFS = new FileWriter("outfile.txt");.- AVerdadero
- BFalso
139Array indices are numbered 1 to N, where N is the total number of elements in an array.
- AVerdadero
- BFalso
140Trying to access an array with an out-of-range index results in a runtime error that causes the program to terminate.
- AVerdadero
- BFalso
141Each item in an array is called an element, and the number in brackets is called the index of the corresponding element.
- AVerdadero
- BFalso
142The following statement is an example of combining the definition and the initialization of an array:
int[] myArray = {5, 2, 3, 7}; .- AVerdadero
- BFalso
143In this case, what is printed out by the line System.out.println(john); ?
- AThe address of where the object is located in memory
- BThe age of the person john
- CThe name of the person john
- DThe name of the person followed by the age of the person
144When will the default Person() constructor be called?
- Awhen no parameters are passed during object creation
- Bwhen the name and age is supplied during object creation
- Cwhen only the age is supplied during object creation
- Dwhen only the name is supplied during object creation
145int value = 1;
while (value < 1) {
System.out.println(value);
value++; }
What logical tests does the while loop perform before ending?- A1 < 1 (first iteration)
2 < 1 (second iteration) - B1<1
- C2<1
- D2 < 1 (first iteration)
1 < 1 (second iteration)
- A1 < 1 (first iteration)
146int value = 1;
do {
System.out.println(value);
value++;
}while(value < 1);
What logical tests does the do-while loop perform before ending?- A1<1 (first iteration)
2<1 (second iteration) - B1<1
- C2<1
- D2<1 (first iteration)
1<1 (second iteration)
- A1<1 (first iteration)
147int sum = 1;
while(sum < 10) {
sum = sum + (sum * 2);
System.out.print(sum + " ");
}
Which do-while loop outputs the same as the above while loop? Assume the variable "sum" is initialized to 1.- Ado {
sum = sum + (sum * 2);
System.out.print(sum + " ");
}while(sum < 10); - Bdo {
sum = sum + (sum * 2);
System.out.print(sum + " ");
}while(sum > 10); - Cdo {
System.out.print(sum + " ");
sum = sum + (sum * 2);
}while(sum < 10);
- Ado {
148do {
System.out.print(sum + " ");
sum = sum + (sum * 2);
}while(sum < 10);
What is the value of "sum" AFTER the second iteration of the loop?- A27
- B3
- C1
- D9
149int sum = 1;
while(sum < 10) {
sum = sum + (sum * 2);
System.out.print(sum + " ");
}
What is the value of "sum" BEFORE the second iteration of the loop?- A27
- B3
- C1
- D9
150In a class definition, the purpose of a constructor is to initialize all the values of the attributes defined in that class (instance variables).
- AVerdadero
- BFalso
151The name of the constructor does not have to be the same as the name of the class.
- AVerdadero
- BFalso
152A default constructor is provided by Java automatically, whenever the programmer does not explicitly code a constructor.
- AVerdadero
- BFalso
153A class constructor can NEVER be overloaded.
- AVerdadero
- BFalso
154The parameter order and data type passed to a constructor must match exactly the order of the arguments passed in the call to the constructor.
- AVerdadero
- BFalso
155After watching the videos at: https://www.youtube.com/watch?v=dtykxBOVgcg look at the code below and then choose the best answer:
String name1 = "jack";
String name2 = "jAcK";
if(name1.equals(name2)) {
System.out.println("The name is Jack!"); }
else {
System.out.println("The name is not exactly the same!");
}- A“The name is Jack!” is the correct output because the spelling is the same.
- B“The name is Jack!” is correct because .equals ignores the case of the word.
- C“The name is not exactly the same!” is the correct output because the name’s cases are different.
- DNeither of these are correct because .compareTo should have been used instead of .equals.
156String name1 = "maNdy";
String name2 = "ManDY";
if(name1.equals(name2)) {
System.out.println("The name is Mandy!");
}
else {
System.out.println("The name is not exactly the same!");
}- A“The name is “Mandy!” is the correct output because .equals was used.
- B“The name is not exactly the same!” is the correct output because the two name’s cases do not match.
- C“The name is “Mandy!” is the correct output because the correct method was used.
- D“The name is not exactly the same!” is the correct output because .equalsIgnoreDifference was not used
157String name1 = "Michael";
String name2 = "miCHaEL";
String name3 = "sanDra";
String name4 = "Sandra";
if (name1.equalsIgnoreCase(name2) && name3.equalsIgnoreCase(name4)) {
System.out.println("The names are Michael and Sandra!"); }
else {
System.out.println("All of these names are different!");
}- A“All of these names are different!” is the correct output because .equals should have been used on the first half of the if statement.
- B“All of these names are different!” is the correct output because .equals should have been used on the second half of the if statement.
- C“The names are Michael and Sandra!” is the correct output because .equalsIgnoreCase was used correctly.
- DNone of these are correct because the name of the correct method is .equalsCaseIgnore.
158Suppose we are appending a file named colors.txt at the same level as our src folder within our project folder. What is the correct format for writing to the file without erasing any of the information the file might already contain?
- AFile aFile = new File ( colors);
Scanner readFile = new Scanner (aFile);
FileWriter writeFile = new FileWriter (“colors.txt”, true);
PrintWriter printFile = new PrintWriter (writeFile); - BFile aFile = new File ( “colors.txt”);
Scanner readFile = new Scanner (aFile);
FileWriter writeFile = new FileWriter (“colors.txt”, true);
PrintWriter printFile = new PrintWriter (writeFile); - CFile aFile = new File ( “colors.txt”);
Scanner readFile = new Scanner (aFile);
FileWriter writeFile = new FileWriter (“colors.txt”, false);
PrintWriter printFile = new PrintWriter (writeFile); - DFile aFile = new File ( “colors.txt”);
Scanner readFile = new Scanner (aFile);
FileWriter writeFile = new FileWriter (“colors.txt”, false);
PrintWriter printFile = new PrintWriter (readFile);
- AFile aFile = new File ( colors);
159Which type(s) of loop(s) are the most suitable for iterating through files from start to finish?
- ADo-While Loop
- BFor Loop
- CWhile Loop
160Which class(es) contain methods that allow us to iterate through data from an input file, with or without knowing the specific type of data in the file?
- AFile
- BPrintWriter
- CFileWriter
- DScanner
161The general format of the System.out.printf() method consists of passing 2 parameters to the method: the format string, and the argument list.
- AVerdadero
- BFalso
162%f is a format specifier that indicates a space will be inserted into the string when displayed.
- AVerdadero
- BFalso
163All format specifiers begin with a % character.
- AVerdadero
- BFalso
164The following statement will display "The number is: 20,000"
System.out.println("The number is:%10.1f", number);- AVerdadero
- BFalso
165What type of loop is most commonly used for populating arrays?
- Ado-while
- Bwhile
- Cfor
166To figure out how large our array is, what property should you check ?
- Asize
- Blength
- Cvalue
- Delements
167Based on the following line of code, what would be the index value of the last element in the array of students?
Students[] roster = new Students[20];- A21
- B20
- C19
- D18
168Look at the following code, what value would be stored in index 2 of the numbers array?
int[] numbers = new int[5];
for(int i = 0; i < numbers.length ; i++ ) {
numbers[i] = i + 1;
}- A1
- B2
- C3
- D4
169Which of the following are valid ways to create an array of integers?
- Aint[] nums = new int[5];
- Bint[] nums =(1)(2)(3)(4)(5);
- Cint[] nums = {1, 2, 3, 4, 5};
- Dint[] nums;
170What do we check on Array Lists to see how many elements are stored inside?
- Alength
- Bvalue
- Csize
- Dweight
171What functionality is easier to accomplish using an arrayList instead of an array?
- Aiterating through all the elements
- Bchanging an element
- Cadding/removing an element
- Daccessing an element
172What would the following code produce?
ArrayList<String> nameList = new ArrayList<>();
nameList.add(“Mark”);
nameList.add(“John”);
nameList.add(“Patricia”);
nameList.set(1, “Mike”);
nameList.add(“Patty”);
nameList.toString();- AMark John Patricia Mike Patty
- BMark Mike Patricia Patty
- CMark John Mike Patty
- DMark Mike John Patricia Patty
173Character literals are enclosed in _________, and string literals are enclosed in ______.
- Asingle quotes, double quotes
- Bdouble quotes, single quotes
- Csingle quotes, single quotes
- Ddouble quotes, double quotes
174The Java API provides a class names MATH, which contains numerous methods that are useful for performing complex mathematical operations.
- AVerdadero
- BFalso
175The BOOLEAN data type may contain the following range of values:
- A-128 to +127
- Btrue OR false
- C-2,147,483,648 to +2,147,483,647
- D-32,768 + +32,767
176This is a named storage location in the computer's memory.
- AOperator
- BConstant
- CLiteral
- DVariable
177What is the result of the following expression?
17 % 3 * 2 - 12 + 15- A105
- B12
- C7
- D8
178What output will be displayed as a result of executing the following code?
int x = 5, y = 20;
x += 32;
y /= 4;
System.out.println("x = " + x ",y = " + y;- Ax = 160, y = 80
- Bx = 32, y = 4
- Cx = 37, y = 5
- Dx = 9, y = 52
179What will be the value of Z after the following statements have been executed?
int x = 4, y = 33;
double z;
z = (double) (y/z);- A8.25
- B4
- C0
- D8.0
180What would be displayed as a result of the following code?
- AThere are 583 hens in the hen house.
- BThere are 5785
hens in the hen house. - CThere are x5\nhens in the hen house.
- DThere are 5785 hens in the hen house.
181Which of the following statements correctly creates a Scanner object for the keyboard input?
- AScanner kbd = new Scanner (System.keyboard);
- BScanner keyboard = new Scanner (System.in);
- CScanner keyboard(System.in);
- DKeyboard scanner = new Keyboard(System.in);
182Which of the following statements will correctly convert the data type, if x is a float and y is a double?
- Ax = float y;
- Bx = <float>y;
- Cx = (float)y;
- Dx = y
183Which Scanner class method reads a String?
- AnextLine
- BcharAt
- CnextString
- DgetLine
184The purpose of pseudocode is:
- ATo explain all the variables that are needed to solve the problem in a program
- BTo indicate the correct sequence of steps to solve the problem in a program, including any decisions that have to be made in the program.
- CTo indicate the inputs and outputs of the program
- DAll of the above
185A method:
- Ais a block of code that is declared in a class.
- Bcan receive parameters that will help the block of code to do its function
- Ccan return a value at the end of the block of code, back to where it was called
- DAll of the above
186A constructor is a special method that is needed in all domain classes, to initialize the values of the instance variables.
- AVerdadero
- BFalso
187Byte code instructions are
- Amachine code instructions
- Bsyntax errors
- Cread and interpreted by the JVM
- Danother name for source code
188The Java Virtual Machine is a program that reads Java byte code instructions and executes them as they are read.
- AVerdadero
- BFalso
189Variables are
- Asymbolic names made up by the programmer whose values cannot be changed
- Boperators that perform operations on one or more operands
- Csymbolic names made up by the programmer that represents locations in the computer's RAM
- Dreserved words
190What is syntax?
- ARules that must be followed when writing a program
- BWords that have a special meaning in the programming language
- CSymbols or words that perform operations
- DWords or names defined by the programmer
191How many bits are in a byte
- A4
- B8
- C16
- D32
192How do you represent 127 in binary?
- A1000 0001
- B0100 0000
- C0111 1111
- D1111 1111
193What is binary number 00001010 = [blank_start]?[blank_end]
- A10
194What is binary number 01010001= [blank_start]?[blank_end]
- A81
195What is 12 in binary[blank_start]?[blank_end] (Use 8 bits to represent)
- A00001100
196What is 35 in binary[blank_start]?[blank_end] (use 8 bits to represent)
- A00100011
197What is 129 in binary[blank_start]?[blank_end] (represent in 8 bits)
- A100000001
198A method's signature consists of
- Athe method name and the parameter list
- Bthe return type, the method name, and the parameter list
- Cthe size of the method in memory
- Dthe return type and the method name
199A static field is created by placing the key word static
- Aafter the access specifier and field's data type
- Bafter the access specifier and before the field's data type
- Cafter the field name
- Din brackets, before the field's data type
200Assume the class BankAccount has been created, and the following statements correctly creates an instance of the class:
BankAccount account = new BankAccount (5000.0);
What is true about the following statement?
System.out.println(account);- AA runtime error will occur
- BThe method will display unreadable binary data on the screen
- Cthe account object's toString method will be implicitly called
- DA compiler error will occur
Información
- Descripción
- Computer Science Test sobre Programming, creado por Jamie M el 29/05/2018.
- Autor
- Jamie M
- Idioma
- English
- Visitas
- 2
- Etiquetas
- Valoración
- Aún sin valoraciones Valora este test