Please note, questions 7, 18, 19, 23 and 31 need a reason for the answer given.
ID: 3770871 • Letter: P
Question
Please note, questions 7, 18, 19, 23 and 31 need a reason for the answer given. Please post answers as :
1. A
2. B
Etc.
Question 1 of 34 (worth 3 points)
Consider the following class definition:
class username
{
private String first;
private String last;
public void getName()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter your first name: ");
first = input.next();
System.out.print("Enter your last name: ");
last = input.next();
System.out.println("");
}
public void display_usernames()
{
System.out.println("These usernames are available for use:");
System.out.println(first + last);
System.out.println(last + first);
System.out.println(first + " " + last);
System.out.println(last + "-" + first);
}
}
Which methods could carry out an assignment first = “Joe”; to the private member variable first?
Check to review before finishing (will be flagged in Table of Contents)
Question 2 of 34 (worth 3 points)
Consider the following method definition that has been properly defined in the class Time.
void shiftBy(int dh, int dm)
{
hour += dh;
minute += dm;
}
public static void main(String[] args)
{
int x = 2;
int y = 4;
Time mytime = new Time();
mytime.shiftBy(x, x);
}
What are the values of hour and minute after the function shiftBy finishes?
Check to review before finishing (will be flagged in Table of Contents)
Question 3 of 34 (worth 3 points)
Consider the following class.
class username
{
private String first;
private String last;
public void getName()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter your first name: ");
first = input.next();
System.out.print("Enter your last name: ");
last = input.next();
System.out.println("");
}
public void display_usernames()
{
System.out.println("These usernames are available for use:");
System.out.println(first + last);
System.out.println(last + first);
System.out.println(first + " " + last);
System.out.println(last + "-" + first);
}
}
Suppose that x and y are both objects of this class. Which of the following statements is legal in the client main program?
Check to review before finishing (will be flagged in Table of Contents)
Question 4 of 34 (worth 3 points)
Consider the following class definition.
class myclass
{
int addition (int a, int b) //method overloading
{
int r;
r= a + b;
return r;
}
void addition (int a, int b, int c)
{
System.out.print("Total is now: ");
System.out.println(a + b + c);
}
}
Another method is created in the same class with the local integer variables x, y and z. Which of the following statements would produce an error if you included it after variables a and b are defined and initialized to 1?
addition(y, x);
addition(x, y);
addition(x, y, z);
addition(x);
Check to review before finishing (will be flagged in Table of Contents)
Question 5 of 34 (worth 3 points)
Consider the following code from a client main method:
public static void main(String[] args) {
Time wells = new Time();
wells.setHour(9); //argument
wells.setMinute(20);
wells.shiftBy(2, 10);
wells.getTime();
wells.countDown(10);
}
Which is an object of the Time class?
Check to review before finishing (will be flagged in Table of Contents)
Question 6 of 34 (worth 3 points)
Which of the following is the correct way to create an object of the CRectangle class?
Check to review before finishing (will be flagged in Table of Contents)
Question 7 of 34 (worth 3 points)
Consider the following code. Note: Line numbers are provided on the left side of the code, but are not part of the file.
1 class myvariables()
2 {
3 private double totalcount;
4 private double n;
5 private String finalstring;
6
7 //TODO: put your class methods here!
8 }
Which line contains a syntax error?
StylesStylesFormatFormatFontFontSizeSize Source
Words: 0
Check to review before finishing (will be flagged in Table of Contents)
Question 19 of 34 (worth 3 points)
Consider the following class definition. Note: Line numbers are provided on the left side of the code, but are not part of the file.
1 public static void mystery(int n1, int n2)
2 {
3 int temp > n1;
4 n1 = n2;
5 n2 = temp;
6 }
Which line contains an error?
StylesStylesFormatFormatFontFontSizeSize Source
Words: 0
Check to review before finishing (will be flagged in Table of Contents)
Question 24 of 34 (worth 3 points)
Which of the following lines of code will display the contents of the variable called first? Assume no errors.
Check to review before finishing (will be flagged in Table of Contents)
Question 25 of 34 (worth 3 points)
Consider the following method implementation for a method that belongs to the myvariables class:
public void getvalues()
{
//local variables
int count = 1;
String first, last;
}
The myvariables class contains a private data member called count that is set to the value 100 before this method is called. If this method included the following line as the last line of this method, what would the result be?
System.out.println(count);
Check to review before finishing (will be flagged in Table of Contents)
Question 26 of 34 (worth 3 points)
Which would you use to add together all of the elements in an array of integers?
Check to review before finishing (will be flagged in Table of Contents)
Question 27 of 34 (worth 3 points)
Which would you use to determine whether the contents of var1 and var2 are not equal?
Check to review before finishing (will be flagged in Table of Contents)
Question 28 of 34 (worth 3 points)
Consider the following method implementation:
void displayScores()
{
int total = 0;
String output = "";
for (int i = 0; i < stats.length; i++)
{
total = total + stats[i];
output = output + stats[i] + " ";
}
//display the total
}
Which type of variable is stats?
Check to review before finishing (will be flagged in Table of Contents)
Question 29 of 34 (worth 3 points)
Consider the following method implementation:
void setHour(int newValue)
{
hour = newValue;
}
Which type of method is setHour?
Check to review before finishing (will be flagged in Table of Contents)
Question 30 of 34 (worth 3 points)
Consider the following method.
public static void swapShells(int n1, int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
The main method contains the following:
public static void main(String[] args)
{
int[] a = {1,2};
swapShells(a[0], a[1]);
}
What is the value in a[0] after these calls are completed?
Check to review before finishing (will be flagged in Table of Contents)
Question 31 of 34 (worth 3 points)
Consider the following method:
int addition (int a, int b)
{
int r;
r=a+b;
}
What is wrong with this method?
StylesStylesFormatFormatFontFontSizeSize
Words: 0
Check to review before finishing (will be flagged in Table of Contents)
Question 32 of 34 (worth 3 points)
Consider the following code:
public static void swapShellsFirstInArray(int[] array)
{
int temp = array[0];
array[0] = array[1];
array[1] = temp;
}
The main method calls the swapShellsFirstInArray method and passes data to it using which of the following?
Check to review before finishing (will be flagged in Table of Contents)
Question 33 of 34 (worth 3 points)
Which access modifier should be used to declare class member variables?
Check to review before finishing (will be flagged in Table of Contents)
Question 34 of 34 (worth 3 points)
A public class is typically stored in a file with which filename extension?
Check to review before finishing (will be flagged in Table of Contents)
A. getName and display_usernames can carry out the assignment, but not main. B. getName and main can carry out the assignment, but not display_usernames. C. getName, display_usernames and main can carry out the assignment. D. Only main can carry out the assignment. E. Neither getName nor display_usernames nor main can carry out the assignment.Explanation / Answer
1.
2.
3
x.display_usernames();
4
C .addition(x, y, z);
5.
wells
6.
CRectangle shape = new CRectangle();
7.
line 1 BECAUSE class myvariables() IS NOT A PROPER WAY OF DECLARING A CLASS . tHE BRACES OF myvariables() SHOULD NOT BE THERE.
19
line 3
because declaration and relational operation should not be in the same statement and comparison is not used like this .Rrelational operation is used in loops , if else statemnet switch etc
24
System.out.println(first);
25
26
27
if(var1 != var2)
29
mutator method
30
1
31
Something is missing.
because the method is int addition(int a,int b) and it should have return value of type int which is missing
33
34
B. getName and main can carry out the assignment, but not display_usernames.Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.