Introduction to Java Programming

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

You have created the following base class of Animals and a subclass of Cat: public class Animal { private int noOfLegs; private String name; public Animal(){} public Animal(int legs, String name) { this.noOfLegs = legs; this.name = name; } public int getNoOfLegs() { return noOfLegs; } public void setNoOfLegs(int noOfLegs) { this.noOfLegs = noOfLegs; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Cat extends Animal { private String color; public Cat(int legs, String name, String color) { super(legs, name); this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void meow() { System.out.println("Meow"); } } What code segment would properly create an instance of the subclass using the variable myCat and name it Garfield with 4 legs and have the color set to Orange?

Cat myCat = new Cat(4, "Garfield","Orange");

You have created the following base class of Animals and a subclass of Dog: public class Animal { private int noOfLegs; private String name; public Animal(){} public Animal(int legs, String name) { this.noOfLegs = legs; this.name = name; } public int getNoOfLegs() { return noOfLegs; } public void setNoOfLegs(int noOfLegs) { this.noOfLegs = noOfLegs; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Dog extends Animal { private String color; public Dog(int legs, String name, String color) { super(legs, name); this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void bark() { System.out.println("Bark"); } } What code segment would properly create an instance of the subclass using the variable myDog and name it Penny with 3 legs and have the color set to White?

Dog myDog = new Dog(3, "Penny","White");

You have created the following base class of Animals and a subclass of Dog: public class Animal { private int noOfLegs; private String name; public Animal(){} public Animal(int legs, String name) { this.noOfLegs = legs; this.name = name; } public int getNoOfLegs() { return noOfLegs; } public void setNoOfLegs(int noOfLegs) { this.noOfLegs = noOfLegs; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class Dog extends Animal { private String color; public Dog(int legs, String name, String color){ super(legs, name); this.color=color; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public void bark() { System.out.println("Bark"); } } What code segment would properly create an instance of the subclass using the variable myDog and name it Snoopy with 4 legs and have the color set to White and Black?

Dog myDog = new Dog(4, "Snoopy","White and Black");

Assume that the logs.txt file does not exist. Given the following snippet of code: File logsFile = new File("logs.txt"); boolean fileDeleted = false; try { fileDeleted = Files.deleteIfExists(logsFile.toPath()); } catch(IOException ex) { System.out.println("Error deleting file: " + logsFile.getName()); } if(fileDeleted) { System.out.println(logsFile.getName() + " deleted."); } else { System.out.println(logsFile.getName() + " not deleted."); } } What would be the output?

Error deleting file: logs.txt logs.txt not deleted.

Assume that the output.txt file contains: First Line Given the following snippet of code: File output = new File("output.txt"); try { Files.writeString(output.toPath(), "Second Line", StandardOpenOption.CREATE, StandardOpenOption.APPEND); Files.writeString(output.toPath(), "Third Line", StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch(IOException ex) { System.out.println("Error: " + ex.getMessage()); } What would output.txt contain?

First LineSecond LineThird Line

Given the following snippet of code: File output = new File("myfile.txt"); try { Files.writeString(output.toPath(), "Java is a programming language\n", StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch(IOException ex) { System.out.println("Error: " + ex.getMessage()); } What would the myfile.txt contain after the code is run?

Java is fun Java is platform independent Java is a programming language

Given the following snippet of code: String fileName = "myfile.txt"; File inputFile = new File(fileName); try { List<String> lines = Files.readAllLines(inputFile.toPath()); System.out.println(inputFile.getName() + " : " + lines.size()); } catch(IOException ex) { System.out.println("File error: " + fileName); } Which file contents in myfile.txt would result in the following output: myfile.txt: 4

Roses are redViolets are blueSugar is sweetand so are you.

Review the class Employee example provided in the tutorial. What code segment could be added to call a method that keeps track of the number of employees managed by Managers, called getNumManaged(), and print this result out for an instance named sophia?

System.out.println(sophia.getNumManaged());

A file was created under the /home/runner/IntrotoJava/ folder named myFile.txt. Assume that you have a File object named testFile pointed to the file. Which segment of code will output: /home/runner/IntrotoJava/myFile.txt

System.out.println(testFile.getAbsolutePath());

Given the following set of classes, what needs to be fixed to make the program produce the output Name: Java? public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } } class CustomerTest { public static void main(String[] args) { Customer c1 = new Customer("user","pass"); c1.setUserName("Java"); outputName(); } public static void outputName(){ System.out.println("Name: " + c1.getUserName()); } }

The instance c1 is not in scope in the method outputName(). The c1 should be globally in CustomerTest to be accessed in the outputName() method.

Given the following set of classes, what would need to be fixed to produce the output Password? public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } } class CustomerTest { public static void main(String[] args) { Customer cust = new Customer("user","pass"); cust.setPassword("Java"); outputPassword(); } public static void outputPassword(){ System.out.println("Password: " + cust.getPassword()); } }

The instance cust is not in scope in the method outputPassword(). The cust should be declared globally in CustomerTest to be accessed in the outputPassword() method.

Given the following set of classes, what would need to be fixed to ensure that the reset of the account executes correctly? public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } } class CustomerTest { public static void main(String[] args) { Customer testAcct = new Customer("user","pass"); testAcct.setPassword("Java"); resetAcct(); } public static void resetAcct(){ testAcct.setPassword(""); testAcct.setUsername(""); } }

The instance testAcct is not in scope in the method resetAcct(). The testAcct should be declared globally in CustomerTest to be accessed in the resetAcct() method.

Assume that the output.txt file contains: First Line Given the following snippet of code: File output = new File("output.txt"); try { Files.writeString(output.toPath(), "Second Line", StandardOpenOption.CREATE); Files.writeString(output.toPath(), "Third Line", StandardOpenOption.CREATE); } catch(IOException ex) { System.out.println("Error: " + ex.getMessage()); } What would output.txt contain?

Third Line

Given the following class, what sets of statements would have the end result of the myUser instance having the username as testuser and the password as testpassword? public class User { private String userName; private String password; public User(String userName, String password) { this.userName = userName; this.password = password; } public String getUserName() { return userName; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; } }

User myUser = new User("username","password"); myUser.setUserName("testuser"); myUser.setPassword("testpassword");

The constructor is used to initialize what components of an instance of an object? a.) Its attributes b.) Its structure c.) Its scope d.) Its methods

a.) Its attributes

What is the term used to bundle the structure and design of code when implementing object-oriented programming? a.) objects b.) classes c.) data collection types d.) methods

a.) objects

Object-oriented programming provides what three traits to programs? a.) reusability, scalability, and efficiency b.) recursion, scalability, and efficiency c.) reusability, simulation, and efficiency d.) reusability, scalability, and inefficiency

a.) reusability, scalability, and efficiency

Subclasses inherit __________ and __________ from their base class.

attributes and methods

Given the following class, what sets of statements would have the end result of the myCustomer instance having the username as XC1001 and the password as testpass? public class Customer { private String customerCode; private String password; public Customer(String customerCode, String password) { this.customerCode = customerCode; this.password = password; } public String getCustomerCode() { return customerCode; } public String getPassword() { return password; } public void setCustomerCode(String customerCode) { this.customerCode = customerCode; } public void setPassword(String password) { this.password = password; } }

b.) Customer myCustomer = new Customer("username","password"); myCustomer.setCustomerCode("XC1001"); myCustomer.setPassword("testpass");

Given the following class, what sets of statements would have the end result of the myStudent instance having the student name as teststudent letterGrade as test as B? public class Student { private String studentName; private String letterGrade; public Student(String studentName, String letterGrade) { this.studentName = studentName; this.letterGrade = letterGrade; } public String getStudentName() { return studentName; } public String getLetterGrade() { return letterGrade; } public void setStudentName(String studentName) { this.studentName = studentName; } public void setLetterGrade(String letterGrade) { this.letterGrade = letterGrade; } } a.) Student myStudent = new Student("B","teststudent"); b.) Student myStudent = new Student("student name","X"); myStudent.setStudentName("teststudent"); myStudent.setLetterGrade("B"); c.) Student myStudent = new Student(); myStudent.setStudentName("teststudent"); myStudent.setLetterGrade("B"); d.) Student myStudent = new Student("teststudent","B"); myStudent.setStudentName("student"); myStudent.setLetterGrade("X");

b.) Student myStudent = new Student("student name","X"); myStudent.setStudentName("teststudent"); myStudent.setLetterGrade("B");

Which type of constructor will initialize the instance variables to initial values without them needing to be set? a.) object b.) default c.) instance d.) parameterized

b.) default

If you created the following interface: interface User { void getUserName(String userName); }

class AccountUser implements User { public void getUserName(String userName) { System.out.println("Account User: " + userName); }

If you created the following interface: interface Language { void getName(String name); } What would be a class that implements the interface correctly?

class ProgrammingLanguage implements Language { public void getName(String name) { System.out.println("Programming Language: " + name); } }

When is the constructor declared for an instance of an object? a.) When an attribute is assigned b.) When a method is called c.) At deletion d.) At class instantiation

d.) At class instantiation

What are the four basic programming structures for writing code in Java? a.) sequential, divisional, repetitive, and reusable b.) sequential, conditional, relational, and reusable c.) consecutive, conditional, repetitive, and disposable d.) sequential, conditional, repetitive, and reusable

d.) sequential, conditional, repetitive, and reusable

For the Employee Class Program in the Company Employee Program tutorial, imagine you need to add in a way to update the salary based on an input of "upd" and the employee ID. What code segment would need to be added to the while loop in the main method to implement this?

else if(cmd.toLowerCase().equals("upd")) { System.out.print("Employee ID to update: "); int emplId = input.nextInt(); input.nextLine(); updateEmployees(csvFile, emplId); }

Which of the following import statements would allow the user to import all the packages and classes under java.util?

import java.util.*;

Which of the following import statements would allow the user to get access to the HashMap object?

import java.util.HashMap;

Which of the following import statements would allow the user to get access to the Scanner object?

import java.util.Scanner;

What concept enables a class to obtain its attributes and methods from another class?

inheritance

In the example of the animal class and its subclasses, which class is the direct base class of feline?

mammal

You have created the following base public class of Animals: public class Animal { private int noOfLegs; private String name; public Animal(){} public Animal(int legs, String name) { this.noOfLegs = legs; this.name = name; } public int getNoOfLegs() { return noOfLegs; } public void setNoOfLegs(int noOfLegs) { this.noOfLegs = noOfLegs; } public String getName() { return name; } public void setName(String name) { this.name = name; } } Assume that the Cat class extends the Animal class. If you create a Cat subclass of Animal in which color is also a String, which constructor accurately does this?

public Cat(int legs, String name, String color) { super(legs, name); this.color=color; }

Which of the following methods can be added to the UserAccount class in the tutorial to access the password?

public String getPassword() { return password; }

Given the following set of classes: public class Client { private String firstName; private String lastName; public Client(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getLastName() { return lastName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getFirstName() { return firstName; } } class ClientTest { public static void main(String[] args) { Client testAcct = new Client("first","last"); testAcct.setLastName("Java"); System.out.println(testAcct.toString()); } } What does the toString() method need to look like to result in the following output: FirstName: firstLastName: Java

public String toString() { String state = "FirstName: " + firstName + "\n"; state += "LastName: " + lastName + "\n"; return state; }

Given the following set of classes: public class Student { private String studentName; private String letterGrade; public Student(String studentName, String letterGrade) { this.studentName = studentName; this.letterGrade = letterGrade; } public void setLetterGrade(String letterGrade) { this.letterGrade = letterGrade; } public String getLetterGrade() { return letterGrade; } public void setStudentName(String studentName) { this.studentName = studentName; } public String getStudentName() { return studentName; } } class StudentTest { public static void main(String[] args) { Student testAcct = new Student("user","A"); testAcct.setLetterGrade("A"); System.out.println(testAcct.toString()); } } What does the toString() method need to look like to result in the following output: StudentName: userLetterGrade: A

public String toString() { String state = "StudentName: " + studentName + "\n"; state += "LetterGrade: " + letterGrade + "\n"; return state; }

For the base public class Person example provided in the tutorial, what if you need to add a subclass for kids on site who attend the daycare that keeps track of their primary parent. What code segment could implement this subpublic class and create an instance of it called kid_1?

public class DaycareKid extends Person{ private String parent; public DaycareKid(String firstName,String lastName,String jobTitle,String parent){ super(firstName,lastName,jobTitle); this.parent = parent; } public String getParent(){ return "Parent: " + parent; } public void setParent(String parent){ this.parent = parent; } } DaycareKid kid_1 = DaycareKid("Johnny","Doe","kid","Jane Doe");

You have created the following class called Job: public class Job { private String role; private long salary; public String getRole() { return role; } public void setRole(String role) { this.role = role; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } } How would you use composition in a Intern class to indicate that an intern has a job and set the role to an Assistant?

public class Intern { private Job job; public Intern() { this.job=new Job(); job.setRole("Assistant"); } }

In the CompanyEmployeesProgram, imagine the CompanyEmployee added a department instance variable. The department needs to be added after the employee's first name. What should the new writeEmployees look like?

public static void writeEmployees(String csvFile, ArrayList<CompanyEmployee> employees) { // Convert ArrayList<CompanyEmployee> to ArrayList<String> ArrayList<String> newEmployees = new ArrayList<>(); for(CompanyEmployee empl : employees) { newEmployees.add(empl.getId() + "," + empl.getSalary() + "," + empl.getLastName() + "," + empl.getFirstName() + "," + empl.getDepartment()); } File outputFile = new File(csvFile); try { // Write to output file in APPEND mode Files.write(outputFile.toPath(), newEmployees, StandardOpenOption.APPEND); } catch(IOException ex) { System.out.println("Error writing to file: " + ex.getMessage()); } }

A file was created under the /home/runner/IntrotoJava/ folder named myFile.txt. Assume that you have a File object named testFile pointed to the file. Which method would identify if the file can be run by the user?

testFile.canExecute()

A file was created under the /home/runner/IntrotoJava/ folder named myFile.txt. Assume that you have a File object named testFile pointed to the file. Which method would identify if the file can be changed?

testFile.canWrite()


Set pelajaran terkait

Research Methods: CITI Training questions #2

View Set

Lecture 14: Contraception for Women

View Set

Week 7: Chapter 43 and Chapter 31 Antihypertensive Agents

View Set