What would be the results after the following code was executed?
int[] x = {23, 55, 83, 19};
int[] y = {36, 78, 12, 24};
for[int a = 0; a < x.length; a++]
{
x[a] = y[a];
y[a] = x[a];
}
a. x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19}
b. x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
c. x[] = {23, 55, 83, 19} and y[] = {23, 55, 83, 19}
d. This is a compilation error
What is the value of scores[2][3] in the following array?
int [] [] scores = { {88, 80, 79, 92}, {75, 84, 93, 80},
{98, 95, 92, 94}, {91, 84, 88, 96} };
a. 94
b. 84
c. 93
d. 95
What will be the result of executing the following code?
int[] x = {0, 1, 2, 3, 4, 5};
a. An array of 6 values ranging from 0 through 5 and referenced by the variable x will be created
b. A compilation error will occur
c. The program will
crash when it is executed
d. The value of x[1] will be 0, x[2] will be 0, x[3] will be 0, x[4] will be 0, x[5] will be 0, and x[6] will be 0.
What would be the results after the following code was executed?
int[] x = {23, 55, 83, 19};
int[] y = {36, 78, 12, 24};
x = y;
y = x;
a. x[] = {36, 78, 12, 24} and y[] = {23, 55, 83, 19}
b. x[] = {36, 78, 12, 24} and y[] = {36, 78, 12, 24}
c. x[] = {23, 55, 83, 19} and y[] = {23, 55, 83,
19}
d. This is a compilation error
Upgrade to remove ads
Only SGD 41.99/year
-
Flashcards
-
Learn
-
Test
-
Match
-
Flashcards
-
Learn
-
Test
-
Match
Terms in this set [65]
This type of method does not return a value.
Void
This appears at the beginning of a method definition.
Header
The body of a method is enclosed in __________.
Curly Braces
A method header can contain __________.
Method modifies, method return type, method name, parameter declarations.
A value that is passed into a method when it is called is known as a[n] __________.
Argument
A variable that receives a value that is passed into a method is known as a[n] __________.
Parameter
This javadoc tag is used to document a parameter variable.
@param
This statement causes a method to end and sends a value back to the statement that called the method.
Return
This javadoc tag is used to document a method's return value.
@return
True or False: You terminate a method header with a semicolon.
false
True or False: When passing an argument to a method, Java will automatically perform a widening conversion [convert the argument to a higher-ranking data type], if necessary.
true
True or False: When passing an argument to a method, Java will automatically perform a narrowing conversion [convert the argument to a lower-ranking data type], if necessary.
false
True or False: A parameter variable's scope is the entire program that contains the method in which the parameter is declared.
false
True or False: When code in a method changes the value of a parameter, it also changes the value of the argument that was passed into the parameter.
false
True or False: When an object, such as a String, is passed as an argument, it is actually a reference to the object that is passed.
true
True or False: The contents of a String object cannot be changed.
true
True or False: When passing multiple arguments to a method, the order in which the arguments are passed is not important.
false
True or False: No two methods in the same program can have a local variable with the same name.
false
True or False: It is possible for one method to access a local variable that is declared in another method.
false
True or False: You must have a return statement in a value-returning method.
true
// This method
has an error! public static void sayHello[];
{
System.out.println["Hello"];
}
The header should not be terminated with a semicolon.
Look at the following method header: public static void showValue[int x] The following code has a call to the showValue method. Find the error. int x = 8; showValue[int x]; // Error!
...
// This method has an error! public static double timesTwo[double num] { double result = num * 2; }
The method should have a return statement that returns a double value
// This method has an error! public static int half[double num] { double result = num / 2.0; return result; }
...
Examine the following method header, and then write an example call to the method: public static void doSomething[int x]
doSomething[25];
Here is the code for the displayValue method, shown earlier in this chapter: public static void displayValue[int num] { System.out.println["The value is " + num]; }>>
...
For each of the following code segments, indicate whether it will successfully compile or cause an error: a. displayValue[100]
...
b. displayValue[6.0]
...
c. short s = 5; displayValue[s];
...
d. long num = 1; displayValue[num];
...
e. displayValue[6.2f];
...
f. displayValue[[int] 7.5];
...
Look at the following method header: public static void myMethod[int a, int b, int c] Now look at the following call to myMethod: myMethod[3, 2, 1]; When this call executes, what value will be stored in a?
The value 3 will be stored in a,
What value will be stored in b?
2 will be stored in b, and
What value will be stored in c?
1 will be stored in c.
What will the following program display? public class ChangeParam { public static void main[String[] args] { int x = 1; double y = 3.4;
System.out.println[x + " " + y]; changeUs[x, y]; System.out.println[x + " " + y]; }
public static void changeUs[int a, double b] { a = 0; b = 0.0; System.out.println[a + " " + b]; } }
...
A program contains the following method definition: public static int cube[int num] { return num num num; } Write a statement that passes the value 4 to this method and assigns its return value to a variable named result.
result = cube[4];
A program contains the following method: public static void display[int arg1, double arg2, char arg3] { System.out.println["The values are " + arg1 + ", " + arg2 + ", and " + arg3]; } Write a statement that calls this method and passes the following variables as
arguments: char initial = 'T'; int age = 25; double income = 50000.00;
...
Write a method named timesTen. The method should accept a double argument, and return a double value that is ten times the value of the argument.
public static double timesTen[double num] { return num * 10.0; }
Write a method named square that accepts an integer argument and returns the square of that argument.
...
Write a method named getName that prompts the user to enter his or her first name, and then returns the user's input.
// Assume java.util.Scanner has been imported. public static String getName[] { String name; Scanner keyboard = new Scanner[System.in]; System.out.print["Enter your first name: "]; name = keyboard.nextLine[]; return name; }
Write a method named quartersToDollars. The method should accept an int argument that is a number of quarters, and return the equivalent number of dollars as a double. For example, if you pass 4 as an argument, the method should return 1.0; and if you pass 7 as an argument, the method should return 1.75.
...
What is the "divide and conquer" approach to problem solving?
A large complex problem is broken down into smaller manageable pieces. Each smaller piece of the problem is then solved.
What is the difference between a void method and a value-returning method?
A void method, which does not return a value, uses the key word void as its return type in the method header. A value-returning method will use int, double, boolean, or any other valid data type in its header.
What is the difference between an argument and a parameter variable?
An argument is a value that is passed into a method when the method is called. A parameter variable is a variable that is declared in the method header, and receives the value of an argument when the method is called.
Where do you declare a parameter variable?
are declared inside the parentheses in the method header. This is often referred to as a parameter list.
Explain what is meant by the phrase "pass by value."
When an argument is passed to a method, only a copy of the argument is passed. The method cannot access the actual argument.
Why do local variables lose their values between calls to the method in which they are declared?
A method's local variables exist only while the method is executing. This is known as the lifetime of a local variable. When the method begins, its local variables and its parameter variables are created in memory, and when the method ends, the local variables and parameter variables are destroyed. This means that any value stored in a local variable is lost between calls to the method in which the variable is declared
Is the following line of code a method header or a method call? calcTotal[];
Method call
Is the following line of code a method header or a method call? public static void calcTotal[]
Method header
import javax.swing.JOptionPane; public class Checkpoint { public static void main[String[] args] { String input; int number;
input =
JOptionPane.showInputDialog["Enter a number."]; number = Integer.parseInt[input];
if [number < 10] method1[]; else method2[];
System.exit[0]; }
public static void method1[] { JOptionPane.showMessageDialog[null, "Able was I."]; }
public static void method2[] { JOptionPane.showMessageDialog[null, "I saw Elba."]; } }
...
What message will the following program display if the user enters 5?
If the user enters 5 the program will display Able was I.
What if the user enters 10?
If the user enters 10 the program will display I saw Elba.
What if the user enters 100?
If the user enters 100 the program will display I saw Elba.
Write a void method that displays your full name. The method should be named myName.
// This is how Mary Catherine Jones would write it. public static void myName[] { System.out.println["Mary Catherine Jones"]; }
What is the difference between an argument and a parameter?
An argument is a value that is passed into a method. A parameter is a special variable that holds the value being passed into the method.
Look at the following method header: public static void myMethod[int num] Which of the following calls to the method will cause a compiler error? a] myMethod[7]; b] myMethod[6.2]; c] long x = 99; myMethod[x]; d] short s = 2; myMethod[s];
b and c will cause a compiler error because the values being sent as arguments [a double and a long] cannot be automatically converted to an int.
Suppose a method named showValues accepts two int arguments. Which of the
following method headers is written correctly? a] public static void showValues[] b] public static void showValues[int num1, num2] c] public static void showValues[num1, num2] d] public static void showValues[int num1, int num2]
Only d is written correctly.
In Java, method arguments are passed by value. What does this mean?
Only a copy of an argument's value is passed into a parameter variable. A method's parameter variables are separate and distinct from the arguments that are listed inside the parentheses of a method call. If a parameter variable is changed inside a method, it has no effect on the original argument.
What will the following program display? public class Checkpoint { public
static void main[String[] args] { int num1 = 99; double num2 = 1.5;
System.out.println[num1 + " " + num2]; myMethod[num1, num2]; System.out.println[num1 + " " + num2]; }
public static void myMethod[int i, double d] { System.out.println[i + " " + d]; i = 0; d = 0.0; System.out.println[i + " " + d]; } }
99 1.5
99 1.5
0 0.0
99 1.5
Look at the following method header. What type of value does the method return? public static double getValue[int a, float b, String c]
double
Write the header for a method named days. The method should return an int and have three int parameter variables: years, months, and weeks.
public static int days[int years, int months, int weeks]
Write the header for a method named distance. The method should return a double and have two double parameter variables: rate and time.
public static double distance[double rate, double time]
Write the header for a method named lightYears. The method should return a long and have one long parameter variable: miles.
public static long lightYears[long miles]
precedence of all operators
- [unary negation] !
* / %
+ -
< > =
== !=
&&
||
= += -= *= /= %=
s
48 terms
dylan_opatz
Chapter 6 Methods
80 terms
nigel_stafford
SIUC CS 202 - Quiz 5
30 terms
emilymirabella
Ch. 3 Comp. Prog Ess. Spring 2017 Questions
26 terms
Jennifer_Hong19
Sets found in the same folderChapter 4 Java Loops and Files
84 terms
NightCloudSurfer
Java chapter 5
20 terms
waking19
Java chapter 6
19 terms
escmacias55
Java chapter 4
26 terms
escmacias55
Other sets by this creatorChapter 4 linux
73 terms
NightCloudSurfer
Chapter 3 linux
52 terms
NightCloudSurfer
Chapter 2 linux
41 terms
NightCloudSurfer
Chapter 1 linux
56 terms
NightCloudSurfer
Verified questionsADVANCED MATH
Order the following events in terms of likelihood. Start with the least likely event and end with the most likely. You randomly select an ace from a regular deck of 52 playing cards. There is a full moon at night. You roll a die and a 6 appears. A politician fulfills all his or her campaign promises. You randomly select the queen of hearts from a regular deck of 52 playing cards. Someone flies safely from Chicago to New York City, but his or her luggage may or may not have been so lucky. You randomly select a black card from a regular deck of 52 playing cards.
Verified answer
ADVANCED MATH
Each day Eastinghouse produces capacitors during three shifts: 8 a.m.–4 p.m., 4 p.m.–midnight, midnight–8 a.m. The hourly salary paid to the employees on each shift, the price charged for each capacitor made during each shift, and the number of defects in each capacitor produced during a given shift are shown in Table 63. Each of the company’s 25 workers can be assigned to one of the three shifts. A worker products 10 capacitors during a shift, but because of machinery limitations, no more than 10 workers can be assigned to any shift. Each day, at most 250 capacitors can be sold, and the average number of defects per capacitor for the day’s production cannot exceed three. Formulate an LP to maximize Eastinghouse’s daily profit [sales revenue – labor cost]. Table 63: $$ \begin{matrix} \text{Shift} & \text{Hourly Salary} & \text{Defects [per Capacitor]} & \text{Price}\\ \text{8 a.m.–4 p.m.} & $\text{12} & \text{4} & $\text{18}\\ \text{4 p.m.–Midnight} & $\text{16} & \text{3} & $\text{22}\\ \text{Midnight–8 a.m.} & $\text{20} & \text{2} & $\text{24}\\ \end{matrix} $$
Verified answer
ADVANCED MATH
The message NOT NOW [numerically 131419131422] is to be sent to a user of the ElGamal system who has public key [37, 2, 18] and private key k = 17. If the integer j used to construct the ciphertext is changed over successive four-digit blocks from j = 13 to j = 28 to j = 11, what is the encrypted message produced?
Verified answer
ADVANCED MATH
Identify the fallacies of relevance committed by the following arguments, giving a brief explanation for your answer. If no fallacy is committed, write “no fallacy.” Something is seriously wrong with high school education these days. After ten years of decline, SAT scores are still extremely low, and high school graduates are practically incapable of reading and writing. The obvious conclusion is that we should close the schools.
Verified answer
Recommended textbook solutionsBook of Proof
2nd EditionRichard Hammack
340 solutions
Numerical Analysis
9th EditionJ. Douglas Faires, Richard L. Burden
873 solutions
Elementary Number Theory
7th EditionDavid Burton
776 solutions
Elementary Differential Geometry
2nd EditionBarrett O'Neill
297 solutions
Other Quizlet setsNutrition t1
15 terms
flaminglilys
BUAD 309 Final
18 terms
nicolegregorio
ANATOMY: Chapter 6 Test
33 terms
ALYSSA_HARDY
Digestive [97-195]
117 terms
los_23
Related questionsQUESTION
printTodaysDate is a method that accepts no arguments and returns no value. Write a statement that calls printTodaysDate.Assume that printTodaysDate is defined in the same class that calls it.
2 answers
QUESTION
How many sig fig. should the answer have when adding or subtracting sig. figs?
9 answers
QUESTION
I. Unusual patterns always indicate the existence of fraud.
15 answers
QUESTION
True or False. The elements inside the for loop control are separated using semicolons instead of commas.
3 answers