CNIT 255

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

(10)What is the output of the following code? public class A { public int compute(int a,int b) { return a + b; } } public class B extends A { @Override public int compute(int a, int b) { return a * b; } } public class Computation { public static void main(String[] args) { A a = new B(); System.out.println(a.compute(10,20)); } }

200

(4)What does the following code print? int x = 1; while(x < 5) { x = 2*x + 1; } System.out.println(x);

7

(1)Which of the following will print the String "hello" in Java? A) System.out.println("hello"); B) printf("hello"); C) "hello".print(); D) println("hello"):

A

(10)If class X extends class Y, which of the following is true? A) X is a Y B) X has a Y C) Y is an X D) Y has an X

A

(2)What best describes the "String[] args" parameter in the main() method? A) It allows one to pass arguments to the program. B) It means the program must be run from the terminal. C) It is used to store String variables while in the main method. D) It's incorrect and will cause a compiler error.

A

(7)Which is true of static instance variables? A) Exist once per class, and represent a single "shared" variable no matter how many instances of a class exist B) Ae constant C) Increase the speed of Java programs

A

(1)Which of the following were keywords that originally described Java? A) Object Oriented B) Architecture-Neutral C) Multithreaded D) All of the above.

D

(2)What keyword would you use to make an int or a double a constant?

Final

(13)The methods setDefaultCloseOperation and setVisible are called on an object of type J...

Frame

(13)GUI stands for...

Graphical User Interface

(14)Name two layouts described in class.

GridLayout, FlowLayout, BorderLayout

(3)What is the output from the following? System.out.println("String".substring(2, 6));

Ring

(10)In the example above: A) class A is the ____class of class B. B) class B is a ____class of class A.

Super, sub

(5)Print all Mondays in December 2019 using a while loop: int firstMondayOfDecember = 2; int daysInAWeek = 7; int daysInDecember = 31; int value = firstMondayOfDecember; System.out.println("Monday dates in December:"); . . . { System.out.println(value); . . . }

while (i<= daysindecember) { system.out.println(i) i+=daysinaweek; }

Fill in the blank in the following function, so that the returned value will be 9: public int test() { int[] a = {1,2,3,4}; int sum = 0; for(int i = . . .; i < a.length; i++) { sum+=a[i]; } return sum; }

1

(6)Mark all that are true of constructors: ❏ Are called when you use the new keyword. ❏ Do not have a return type. ❏ Must have the same name as the class they're defined in. ❏ Can only have a single parameter.

1,2,3

(7)Given the following function: void doubleValue(int value) { value=value*2; } What will be the value of the number after the following code is executed? int number = 10;

10

(3)What is the output from the following? System.out.println("12" + "13");

1213

(5)What is the output of the following main function? public static void main(String[] args) { int[] primes = {11, 13, 17, 19, 23, 29}; System.out.println(primes[1]); System.out.println(primes[5]); }

13 29

(5)How would you get the length of an array stored in a variable called arr? A) arr.length B) arr.length() C) arr.size

A

(2)Which conversions are automatic in Java? Choose all that apply: A) int => double B) double => int C) char => int D) long => int E) double => short

A & C

(10)Read the following example. A) How does it use encapsulation? B) How does it use inheritance? C) How does it make polymorphism possible? abstract class Animal{ public abstract void makeSound(); } class Dog extends Animal{ private static final String sound = "bark"; @Override public void makeSound() { bark(); } public void bark(){ System.out.println(sound); } } class Cat extends Animal{ private static final String sound = "meow"; @Override public void makeSound() { meow(); } public void meow(){ System.out.println(sound); } }

A: dog and cat are both private B: dog and cat extend abstract class C. can use makeSound no matter if it was dog or cat as long as it was animal

(9)Mark all statements that are valid "best practices" to use when writing code: ❏ Attempt to minimize the number of variables in your classes. ❏ Mark variables private where possible. ❏ Always initialize data. ❏ Break up classes with too many responsibilities into two or more other classes.

All

(12)What are 3 differences between an array and arraylist in Java?

ArrayList has dynamic size, can only contain objects, and has methods

(13)Which of the following is not true: A) To create your own frame, you should extend JFrame B) Make the frame visible by calling frame.showMe(true); C) Code that modifies the GUI should be placed on the event queue

B

(15)What value will be returned when calling mystery5()? public static String swapAndChop(String a, String b){ String tmp = b; b = a; a = tmp; return a.substring(1) + b.substring(1);} public static String mystery5(){ String first = "Abraham"; String last = "Lincoln"; String intermediate = swapAndChop(first, last); return first + " " + intermediate.substring(0, 1);} A. "Abraham L" B. "Abraham i" C. "Lincoln i" D. "Lincoln L"

B

(15)How can you add plain-English comments to code in Java? A. By putting it between /* and */ anywhere in the file B. By putting it after // at the end of a line. C. Both A and B D. None of the above

C

(15)Which of the following statements is correct? A. A final class cannot be public. B. A public class cannot be final. C. A final class cannot be extended. D. final is not a valid class modifier.

C

(3)Which of the following is true of Java Strings? A) It is a primitive variable type in Java B) You can create a String by wrapping it in single quotes (') C) You can create a String by wrapping it in double quotes (") D) Strings are mutable

C

(3)What does it mean that Strings are immutable?

Cannot be changed

(13)In order to create a component, you must extend the J. . . class.

Component

(3)What is the output of each System.out.println in the following program? (Hint: If you're not sure how a String function works, Google java string api.) public class StringMethods { public static void main(String[] args) { String str = "Java Fundamentals"; System.out.println(str.charAt(5)); System.out.println(str.startsWith("java")); System.out.println(str.endsWith("tals")); System.out.println(str.indexOf("men")); System.out.println(str.lastIndexOf("a")); System.out.println(str.replace("Fund", "Coll")); System.out.println(str.substring(3)); System.out.println(str.substring(1, 5)); System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase()); } }

F False True 10 14 Java Collamentals a Fundamentals ava java fundamentals JAVA FUNDAMENTALS

(12)True or false: when using add(), you can only add an element to the end of an arraylist.

False

(2)If the Java file MyClass.java is compiled successfully, what file is produced?

MyClass.class

(10)Can an abstract class be instantiated?

No

(6)Look at the following class. Is it well encapsulated? public class BankAccount { public String accountHolder; public float money; public BankAccount(String accountHolder, float money) { this.accountHolder = accountHolder; this.money = money; } }

No

(7)Why does it make sense that the main method is static?

No objects exist yet upon which we can call a regular method when the program first starts

(6)What do you think will happen when this code is executed? Point x = null; Point x2 = new Point(3, 3); System.out.println("The two x coordinates added: " + x.getX() + x2.getX());

NullPointerException

(10)What keyword can you use to ensure you are overriding a method correctly?

Override

(9)How can you group related classes in Java?

Packages

(11)What is an abstract function?

Technically has no function, used when subclasses are used to provide details

(6)What is a benefit of marking variables "private"?

They are read-only

(14)True or False: A JPanel is an invisible container that holds components.

True

(9)True or False: all classes belong to some package.

True

(9)True or False: two classes are allowed to have the exact same name as long as they are in different packages.

True

(12)What is a situation when you prefer an arraylist over array?

When you're not sure of how many elements you'll need beforehand

(5)In the following variable declarations, what is the difference between x and y? int x; int y[];

Y is an array

(14)In the Model-View-Controller pattern: a. The . . . displays the data. b. The . . . handles events. c. The . . . is pure data with no visual appearance.

a) View b) Controller c) Model

(4)Would an if/else chain or a switch statement be better for each of these programs? Why? a) Performing a specific action based on character input from the keyboard. 'q' - quit. 'c' - continue. 'y' - yes. 'n' - no. 'h' - help. b) Changing power level based on temperature data from a thermometer every 5 minutes: Below -10°C --> 100%. Between -10°C and 10°C --> 80%. Between 10°C and 25°C --> 60%. Greater than 25°C --> 40%.

a) switch is often preferable when you need to choose between multiple predefined values, based on 1 variable. b) if/else because you can't use conditions with switch. But they are easy to use with if/else.

(2)What is the output of the following code? public class SimpleOperations { public static void main(String[] args) { double a = 10.5 % 2; double b = 3.5 * 2; double c = 6.8 / 2; System.out.println("a => " + a + " b => " + b + " => c " + c); } }

a=>0.5 b=>7.0 =>c 3.4

(4)What is the difference between the break and continue keywords?

break will immediately exit the loop; continue will immediately start the next iteration of the loop

(2)Name the two floating type variables in Java:

float and double

(5)Fill in the missing code: Print all days of the week using a regular for loop: String[] weekDays = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; . . . { String day = . . . System.out.println(day); }

for (int i = 0; i<daysinaweek.length; i++) { string day = daysinaweek[i]; system.out.println(day) }

(9)How would you import every class in the java.util package without importing each one, one at a time?

import java.util.*;

(12)How should we test if 2 integer onjects, num1 and num2, are equal?

num1.equals(num2)

(9)Add get / set methods to this class. Name should be read-only. Age should be both readable and writeable. (A get method returns a variable's value. A set method sets a variable's value.) public class Person { private String name = "MyName"; private int age = 25; }

public String getName() { return name; } public int getAge() { return age; } public void setAge(int Age) { this.age=age; }

(1)Write a simple program that has the following features: Has the class name "MyFirstJavaApp". Prints your name out. Then prints out "The current year is 2021". Then answer the following questions: a)What command do you use to compile this program? b)What command do you use to run this program? c)What is the output of the program?

public class MyFirstJavaApp{ public static void main(String [] args) { System.out.println("Emagen Grubbs") System.out.println("The current year is 2021") }} a. javac b. java MyFirstJavaApp c. > Emagen Grubbs The current year is 2021

(6)Read the following classes: import java.time.LocalDate; class Employee { // Fields private String name; private double salary; private LocalDate hireDay; // Constructors public Employee(String n, double s, int year, int month, int day) { name = n; salary = s; hireDay = LocalDate.of(year, month, day); } // Methods public String getName() { return name; } } public class EmployeeMaker {___________________________ } Within the EmployeeMaker class, write a main function that: A) Creates a new Employee object with your name, desired salary, and birthdate. B) Prints out your name using the Employee object you created. C) What happens if an invalid year, month, and day combination is specified when you create a new Employee object? (Hint: See the Java API for LocalDate.of)

public static void main (String[] args){ employee em = new Employee("Emagen Grubbs", 100000, 2002, 7, 11); System.out.println("em.getname()); } Date time exception error


Set pelajaran terkait

Exam #2 Chapter Review Questions

View Set

French Units 1 - 4 September 28, 2016 Study Guide

View Set

ECO202 Chp5 Externalities, Environmental Policy, and Public Goods

View Set

Abeka: Themes in Literature Reading Quiz N

View Set

Chapter 26: Drugs Used to Treat Peripheral Vascular Disease

View Set

Chapter 4, Health of the Individual, Family, and Community

View Set