Docsity

Introduction to Java Programming and Data Structures – 12th Edition, Y. Daniel Liang, Exams of Java Programming

Toggle sidebar
bg1
Introduction to Java Programming and
Data Structures – 12th Edition, Y. Daniel
Liang
Which of the following encapsulates a program's data and its operations? - Correct answer -an object
Consider the necessary tasks involved in changing a flat tire:
1. place new tire and tighten bolts
2. remove the tire
3. loosen bolts and lift car
4. get your tools
Place these steps in their logical order. - Correct answer -4, 3, 2, 1
What type of operation is used to perform tasks on data including calculations? - Correct answer -
processing
Why does a programmer add comments (//) when coding an algorithm? - Correct answer -Used to
explain how the program works or functions. These lines are ignored by the compiler during runtime.
Which pane in the default layout configuration of Replit contains the code editor? - Correct answer -
Middle Pane
What would be the result of the following code being executed?
class Main {
public static void main(String[] args) {
System.out.println(I am learning Java);
}
} - Correct answer -There would be an error due to the missing double quotes around the string value I
am learning Java.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Introduction to Java Programming and Data Structures – 12th Edition, Y. Daniel Liang and more Exams Java Programming in PDF only on Docsity!

Data Structures – 12th Edition, Y. Daniel

Liang

Which of the following encapsulates a program's data and its operations? - Correct answer -an object Consider the necessary tasks involved in changing a flat tire:1. place new tire and tighten bolts

  1. remove the tire3. loosen bolts and lift car
  2. get your toolsPlace these steps in their logical order. - Correct answer -4, 3, 2, 1

What type of operation is used to perform tasks on data including calculations? - Correct answer -processing

Why does a programmer add comments (//) when coding an algorithm? - Correct answer -Used toexplain how the program works or functions. These lines are ignored by the compiler during runtime.

Which pane in the default layout configuration of Replit contains the code editor? - Correct answer -Middle Pane

What would be the result of the following code being executed?class Main { public static void main(String[] args) {System.out.println(I am learning Java);

} - Correct answer -There would be an error due to the missing double quotes around the string value I} am learning Java.

Data Structures – 12th Edition, Y. Daniel

Liang

What is the data type in Java that consists of a true or false value? - Correct answer -boolean Which of the following would create a data type that contains the value sophia? - Correct answer -Stringname = "sophia";

What would be used to find the remainder of 12 divided by 5 (using integer division), and what would bethe result? - Correct answer -12%5, 2

Which of the following error types would be generated, if a program expected to have a variable setprior to use? - Correct answer -Runtime

Which of these programs would produce the output 13test? - Correct answer -int num1 = 5;int num2 = 8; char val = 'A';String word = "test"; System.out.println(num1 + num2 + word); What are the actual variable names specified to pass values into a method when we define a method? -Correct answer -parameter

Given the following lines of code, which of the following is the result of a user entering 8 whenprompted? int wholeNumber = input.nextInt(); System.out.println("Your number is " + wholeNumber + "."); - Correct answer -The string "Your numberis 8." is output to the screen.

Which of the following would output do re mi to the screen? - Correct answer -String firstString= "do ";

Data Structures – 12th Edition, Y. Daniel

Liang

} else {^ System.out.println("Indoor sport."); System.out.println("Outdoor sport.");} } - Correct answer -Indoor sport. Which of the following happens in a program if there is an error that is not handled by a try/catchstatement to catch that exception specifically? - Correct answer -The program will terminate and raise an error Apply what you have learned in Unit 1 about data types, operands, functions, and conditions in simpleJava programs to answer the following question: This Java snippet checks whether a number is even or odd. Which method would make the most sensefor line 2? 1 Scanner input = new Scanner(System.in);2 int age = input.______; 3 if (age _____ == 0){4 System.out.println("Your age is even."); 5 } else {6 System.out.println(________); 7 } - Correct answer -nextInt() Which of the following options is a property of Java arrays? - Correct answer -Data items in an arrayneed to be of the same type.

String[] letters = {"z", "d", "b", "g", "a", "y"}; Arrays.sort(letters);

Data Structures – 12th Edition, Y. Daniel

Liang

System.out.println(letters[1]); - Correct answer -b What would be the result of the following code snippet?int[] ages = {50, 40, 30, 20, 10}; Arrays.sort(ages);int[] agesOther = Arrays.copyOfRange(ages, 0, ages.length-2); System.out.println(Arrays.toString(agesOther)); - Correct answer -[10, 20, 30] What is the primitive data type that is typically used to store single letters, digits, and symbols? - Correctanswer -char

The __________ is a collection that stores key-value pairs. - Correct answer -HashMap Suppose you have an array defined in the following way: int myArray[][] = {{16, 719, 7}, {6, 5, 4}, {19, 3, 1}};What are the correct dimensions of this array? - Correct answer -3 x 3

What is the result of this code? ArrayList flowers = new ArrayList<>();flowers.add("daisy"); flowers.add("rose");flowers.add("orchid"); System.out.println(flowers.get(0));System.out.println(flowers.get(1)); System.out.println(flowers.get(2)); - Correct answer -daisy roseorchid

Data Structures – 12th Edition, Y. Daniel

Liang

What would be output at the end of the following code segment?int num = 0; for(int row = 0; row < 4; row++ ) {for(int col = 0; col < 5; col++) { num++;} }System.out.println(num); - Correct answer -

What is wrong with the following code segment if the numbers from 1 to 10 are meant to be output? int num = 0;while (num < 10) num = num + 1; System.out.println(num); - Correct answer -Nothing is printed in the while loop. With the tic-tac-toe game, why was the for loop used to get the player inputs? - Correct answer -Therewas a definite set of selections for the board.

Given the following method: public static double getAverage(double... values) { double sum = 0;int count = 0; for(double number : values) {sum += number; count++; }return sum / count;

Data Structures – 12th Edition, Y. Daniel

Liang

What would avg1 be set to after the following statement?double avg1 = getAverage(18, 6, 21.3); - Correct answer -15.

Given the name of the following method, what would be the most accurate return statement in terms ofthe return variable or expression? - Correct answer -sum

static String saySomething(String name) { return "Hi " + name; } static String saySomething(String name,int count) { String greeting = ""; for(int i = 0; i < count; i++) { greeting += "Welcome, " + name + "\n"; } return greeting; }Which of the following would need to be called to result in the output:

Hi, Sophia - Correct answer -System.out.println(saySomething("Sophia")); What is the issue? static String saySomething(String name, int count) { String greeting = ""; for(int i = 0; i < count; i++) {greeting += "Welcome, " + name + "\n"; } return name; } - Correct answer -The return statement.

Given the following method, what would be returned given the following board state when checking forO?

{{" X ", " O ", " O "}, {" - ", " X ", " - "}, {" - ", " - ", " X "}} public static int checkForDiagonalWin(String[][] board, char player) { int diagonalNumber = 0;if(board[1][1].equals(" " + player + " ")) { if(board[0][0].equals(" " + player + " ") && board[2][2].equals(" " + player + " ")) { diagonalNumber = 1; } else if(board[0][2].equals(" " + player + " ") &&board[2][0].equals(" " + player + " ")) { diagonalNumber = 2; } } return diagonalNumber; } - Correct

Data Structures – 12th Edition, Y. Daniel

Liang

Subclasses inherit __________ and __________ from their base class. - Correct answer -attributes andmethods

What code segment would properly create an instance of the subclass using the variable myDog andname it Penny with 3 legs and have the color set to White? - Correct answer -Dog myDog = new Dog(3, "Penny","White"); If you created the following interface: interface Language { void getName(String name); } What would be a class that implements the interface correctly? - Correct answer -classProgrammingLanguage implements Language { public void getName(String name) { System.out.println("Programming Language: " + name); } } How would you use composition in a Person class to indicate that a person has a job and set the role toa Programmer? - Correct answer -public class Person { private Job job; public Person() { this.job=new Job(); job.setRole("Programmer");} } Assume that the Dog class extends the Animal class. If you create a Dog subclass of Animal in whichfavoriteTreat is also a String, which constructor accurately does this? - Correct answer -public Dog(int legs, String name, String favoriteTreat) { super(legs, name); this.favoriteTreat=favoriteTreat; } For the subpublic class Contractor of the base public class Person example provided in the tutorial, whatif you need to keep track of the project for each contractor.

Data Structures – 12th Edition, Y. Daniel

Liang

What code segment could be used to properly initialize and add methods to set and get this value? -Correct answer -public class Contractor extends Person { private int contractorId; private double wage; private String project; public Contractor(String firstName,String lastName,String jobTitle,double wage,intcontractorId,String project) { super(firstName,lastName,jobTitle); this.contractorId = contractorId; this.wage = wage; this.project = project; } public String getProject() { return "Project: " + project; } publicvoid setProject(String project) { this.project = project; } }

Which of the following import statements would allow the user to get access to the Scanner object? -Correct answer -import java.util.Scanner;

A file was created under the /home/runner/IntrotoJava/ folder named myFile.txt. Assume that you havea File object named testFile pointed to the file. Which method would identify if the file can be changed? - Correct answer -testFile.canWrite() Assume that the output.txt file contains:First Line Given the following snippet of code:What would output.txt contain? - Correct answer -First Line Second LineThird Line

A myfile.txt has the following lines of text, with each line of text having a new line after it: Roses are red Violets are blue Given the following snippet of code: - Correct answer -Roses are redViolets are blue

Data Structures – 12th Edition, Y. Daniel

Liang

For the craps game in the tutorial, the client may want to enter a cash amount to bet for each game. Ifthey bet (e.g., $5) and win, they receive two times their bet ($10); if they lose, the money they bet (e.g., $5) goes to the casino.If you need to add the core logic to allow a player to bet in this way, what pseudocode accurately represents this task? - Correct answer -# Ask the player how much to bet per game# Play the game

If the player won the game add double the bet to the total won# else the player lost so subtract the bet from the total won

For the craps game in the tutorial, if code were implemented to input a bet for a player, what codesegment would ensure that a non-negative number bet is received? - Correct answer -// Track if input of bet is valid int betAmount = -1; // Assume invalid input of number until it proves to be correctwhile(!betAmount < 0) { try { System.out.print("Pass Line Bet : "); betAmount = input.nextInt(); if (betAmount >= 0) playMultipleGames(number,betAmount); else System.out.println("Please enter a non-negative number for your bet."); } catch(InputMismatchcatchion ex) { System.out.println("Not a valid bet. Please enter a number for your bet."); // Clear out input to remove \n input.nextLine(); } }System.out.println(); // Add blank line in output

At a minimum, it is a best practice that comments be made for which of the following? - Correct answer-Conditional statements

Have you completed your Java Journal? - Correct answer -Yes