Introduction to Programming with Python & Java: Part 2

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What will be the output of the following program? class Main { public static void main(String[] args) { HashSet<String> set = new HashSet<String>(); set.add("Hello"); set.add("Hello"); set.add("Goodbye"); for (String s: set) { System.out.print(s); } } Select all that apply A. "HelloGoodbye" B. "HelloHelloGoodbye" C. "GoodbyeHello" D. "GoodbyeHelloHello"

A and C

What is the result of the following code? abstract class Base { void fun() { System.out.println("Base fun() called"); } } class Derived extends Base {} class Main { public static void main(String args[]) { Base b = new Derived(); b.fun(); } } A. "Base fun() called" B. An error message. C. Nothing will be printed out.

A. "Base fun() called"

If you want your condition to depend on both conditions being true, what is the proper notation? A. && B. || C. and D. !

A. &&

How do you create an instance of the class CIT? A. CIT cit590 = new CIT(); B. def cit590 = CIT(); C. CIT cit590 = CIT(); D. CIT cit 590 = new cit590():

A. CIT cit590 = new CIT();

Which of the following is true if a class Shape has a subclass Circle? A. Circle can have no other parent than Shape. B. Circle can have no subclasses. C. Circle can have only one subclass.

A. Circle can have no other parent than Shape.

What is the result of the following code? ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add(0, "D"); list.add("C"); System.out.print(list.get(0)); System.out.print(list.get(2)); A. DB B. DC C. AC D. AB

A. DB

What is the result of the following code (public static void main(String[] args) { int[] arr = {10, 20, 30, 40, 50}; for(int i = 0; i <= 5; i++) { System.out.print(" " + arr[i]); } })? A. Error Message B. 10 20 30 40 50 C. 10 20 30 40 D. 1 2 3 4 5

A. Error Message

Where is an object's data stored? A. Fields B. Class C. Constructors D. Methods

A. Fields

Which type of Set would you use if you 1.) desire fast access and do not care about ordering and 2.) need elements sorted. A. HashSet, TreeSet B. HashSet, HashSet C. TreeSet, TreeSet D. TreeSet, HashSet

A. HashSet, TreeSet

What happens if BufferedReader's readLine() method has nothing more to read? A. It will return null B. It throws an IOException

A. It will return null

Which of the following allows us to deep dive into the execution of a method call, from within another method? A. Step Into B. Step Over C. Step Return D. Resume

A. Step Into

Which of the following types is not a primitive data type in Java? A. String B. int C. boolean D. char

A. String

A static method cannot access an instance variable. A. True B. False

A. True

A subclass can inherit both instance variables and methods. A. True B. False

A. True

All modern I/O is stream-based. A. True B. False

A. True

An instance method can call a static method or access a static variable. A. True B. False

A. True

Can we use the Scanner class to read a file? A. True B. False

A. True

Fields are available throughout the entire class that declares them. A. True B. False

A. True

Is 'String printSomething(String i) { ... }' a valid overloaded method? A. True B. False

A. True

Is 'int printSomething(int i, int j) { ... }' a valid overloaded method? A. True B. False

A. True

Is 'void printSomething(String i) { ... }' a valid overloaded method? A. True B. False

A. True

Is 'void printSomething(char i, int j) { ... }' a valid overloaded method? A. True B. False

A. True

The keyword this cannot be used in a static method. A. True B. False

A. True

You can have multiple "catch" blocks in a Java try-catch statement. A. True B. False

A. True

You use the class name to call a static method. A. True B. False

A. True

How do you get the size of an array and an ArrayList respectively? A. array.length, arrayList.size() B. array.size(), arrayList.length C. arrayList.length, arrayList.length D. array.size(), array.size()

A. array.length, arrayList.size()

Which of the following methods checks if two object references point to the same object? A. assertSame B. assertCheck C. assertEqual D. assertEquals

A. assertSame

When Eclipse and the JUnit framework creates new test method stubs for you, what is automatically filled in and what is the reason for that? A. fail; so the test fails until the programmer writes it B. fail; because Java is annoying C. pass; so the test fails until the programmer writes it D. pass; so the test passes until the programmer writes it

A. fail; so the test fails until the programmer writes it

Which statement is correct in syntax? A. int myInt = 2; B. myInt = 2; C. int myInt = 3 D. int myInt = "4";

A. int myInt = 2;

Which of the following methods of the Scanner class is used to read integers from a file? A. nextInt() B. scanf() C. write() D. getInteger()

A. nextInt()

Which of the following must be used for the main() method? A. public B. private C. protected D. None of the above

A. public

What is the result of the following code? abstract class Base { void fun() { System.out.println("Base fun() called"); } } class Derived extends Base { @Override void fun() { System.out.println("Fun overwritten!"); } } class Main { public static void main(String args[]) { Base b = new Derived(); b.fun(); } } A. "Base fun() called" B. "Fun overwritten!" C. An error message.

B. "Fun overwritten!"

What will the following code output? String a = "Hello"; char b = '!'; int c = 0; System.out.println(a + b + c); A. Compile error B. "Hello!0" C. "Hello" D. "0"

B. "Hello!0"

What is the output of the code below? abstract class Beverage { abstract void pour(); } class Tea extends Beverage { @Override void pour() { System.out.println("Pouring Tea"); } } class Coffee extends Beverage { @Override void pour() { System.out.println("Pouring Coffee"); } } class Test { public static void main(String[] args) { Beverage tea = new Coffee(); tea.pour(); } } A. "Pouring Tea" B. "Pouring Coffee" C. Nothing will be printed out. D. An error will be raised.

B. "Pouring Coffee"

What will be the output of the following program? class A { public void display() { System.out.print("One"); } } class B extends A { @Override public void display() { System.out.print("Two"); } public class C { public static void main(String args[]) { B screen = new B(); screen.display(); } } A. "One" B. "Two" C. "OneTwo" D. "TwoOne"

B. "Two"

Which of these packages contain classes and interfaces used for input and output operations? A. 'java.util' B. 'java.io' B. 'java.lang'

B. 'java.io'

What will be the output of the following program? abstract class Calculation { abstract void calculate(int number); } class Summation extends Calculation { int result; @Override public void calculate(int number) { this.result = number + number; } } class Main { public static void main(String args[]) { Summation summation = new Summation(); summation.result = 0; summation.calculate(2); System.out.print(summation.result); } } A. 2 B. 4 C. 0 D. Compile error

B. 4

What is the length of the array arr (int[] arr = {0, 1, 2, 3, 4, 5, 6};)? A. 0 B. 7 C. 6 D. Error Message

B. 7

What types of comparisons should we use for each data type? A. .equals for primitives, == for objects B. == for primitives, .equals for objects C. == for primitives and objects D. .equals for primitives and objects

B. == for primitives, .equals for objects

If you want to run a setUp method before every test method, what should you write? A. @Test B. @BeforeEach C. @return D. @BeforeThis

B. @BeforeEach

What will happen during execution of a Java program if a negative int is used for an array index? A. The first slot of the array will be used B. An IndexOutOfBoundsException will be thrown C. A NumberFormatException will be thrown D. None of the above

B. An IndexOutOfBoundsException will be thrown

What will be the output of the following code? public static void main(String[] args) { try { int b = 0; int a = 2 / b; System.out.println("A"); } catch (ArithmeticException e) { System.out.println("B"); } finally { System.out.println("C"); } A. A, B, C B. B, C C. A, C D. A, B

B. B, C

What class can be used to buffer the data sent to a FileWriter stream? A. Buffer B. BufferedWriter C. Writer D. BufferedFileWriter

B. BufferedWriter

What does the following code do? File file = new File("main.txt"); FileWriter fileWriter = new FileWriter(file, false); A. Create a new "main.txt" in the current directory or open a previous "main.txt" for appending. B. Create a new "main.txt" in the current directory. C. Create a new "main.txt" in the current directory for appending. D. None of the above

B. Create a new "main.txt" in the current directory.

A class that is declared as abstract must have at least one abstract method. A. True B. False

B. False

A non-abstract class can contain an abstract method. A. True B. False

B. False

An abstract class can only contain abstract methods. A. True B. False

B. False

The println() method of the PrintWriter class throws an exception. A. True B. False

B. False

We can instantiate an abstract class. A. True B. False

B. False

We cannot declare private variables in a child class because its parent class should have access to every variable in the child class. A. True B. False

B. False

You can have two overloaded methods with a difference only in the return type. A. True B. False

B. False

You can write a Java program without creating one or more classes. A. True B. False

B. False

If you want to define a char first, and then a String, what is the correct type of quotation marks (and order) to use? A. First " ", then " " B. First ' ', then " " C. First ' ', then ' ' D. First " ", then ' '

B. First ' ', then " "

Which collection does not extend the Collection interface? A. List B. Map C. Set

B. Map

A method signature consists of: A. Method Name, Return Type, and Number of Arguments B. Method Name, Number of Arguments, Types of Arguments, and Order of Arguments C. Access Modifier, Method Name, and Types of Arguments D. Return Type, Access Modifier, and Order of Arguments

B. Method Name, Number of Arguments, Types of Arguments, and Order of Arguments

Are elements in a HashSet ordered? A. Yes B. No

B. No

Can a subclass inherit a private member of its parent class? A. Yes B. No

B. No

What kinds of data type(s) can we store in a Collection? A. Primitive values B. Objects C. Both of the above

B. Objects

What data type does the readLine() method in the BufferedReader class return? A. char B. String C. List of String

B. String

Which of the following matches "one letter or underscore followed by one or more letters, digits, and/or underscores" using a regular expression in Java? A. [a-z_][a-z_0-9]+ B. [a-z_A-Z][a-z_A-Z0-9]+ C. +[a-z_A-Z][a-z_A-Z0-9] D. [a_zA_Z][a_zA_Z0_9]+

B. [a-z_A-Z][a-z_A-Z0-9]+

Which fields in class A can be inherited in class B in the code below if A and B are in the same package? class A { protected int a; public int b; private int c; int d; } class B extends A {} A. a, b, c, d B. a, b, d C. b, d D. a, b

B. a, b, d

What is the result of the following code? public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 100); map.put("b", 200); map.put("c", 300); map.put("d", 400); Set<Map.Entry<String, Integer>> mapSet = map.entrySet(); for (Map.Entry<String, Integer> entrance : mapSet) { System.out.print(entrance.getKey() + ":"); System.out.println(entrance.getValue()); } } A. a:100 B. a:100, b:200, c:300, d:400 C. An error message

B. a:100, b:200, c:300, d:400

What will the following code result in? int num = 6.7; A. num being 6 B. compile error C. nothing D. num being 6.7

B. compile error

Choose the appropriate data type for this value: 5.5 A. int B. double C. String D. None of the above

B. double

How would you define a function getSum which returns an int value? A. void getSum() { } B. int getSum() { } C. int getSum{} D. String getSum() { }

B. int getSum() { }

Which of the following is correct to create an array in Java? A. String[] strings = new String[]; B. int[] primes = {2, 3, 5, 7, 11, 13, 19}; C. int[] a = {2.1, 3, 10}; D. boolean[] b = boolean[2];

B. int[] primes = {2, 3, 5, 7, 11, 13, 19};

Which one of the following methods in the Scanner class is used to read integers from a file? A. scanf() B. nextInt() C. get() D. getInteger()

B. nextInt()

Which method of an Exception object prints the error message and information about which line? A. traceStack() B. printStackTrace() C. getMessage() D. printErrors()

B. printStackTrace()

Which type of variable or method can only be accessed from within the same package or sub classes in a different package? A. public B. protected C. private D. default

B. protected

What is the correct way to create a constructor for a Book class? A. public int Book() { } B. public Book() { } C. public Book { } D. public void Book() { }

B. public Book() { }

What is the correct syntax for Java's main method? A. public void main() B. public static void main(String[] args) C. public static void main(string[] args) D. public static void main()

B. public static void main(String[] args)

What will the following code output? System.out.println(5 > 4); A. True B. true C. False D. false

B. true

What will be the output of the following program? abstract class Calculation { abstract void calculate(int number); } class MySummation extends Calculation { int result; @Override public void calculate(int number) { if (number < 10) { this.result = 590; } else { this.result = number + number; } } } class Main { public static void main(String args[]) { MySummation mySummation1 = new MySummation(); mySummation1.calculate(9); MySummation mySummation2 = new MySummation(); mySummation2.calculate(10); System.out.print(mySummation1.result); System.out.print(", "); System.out.print(mySummation2.result); } } A. "590, 590" B. "18, 20" C. "590, 20" D. Compile error

C. "590, 20"

What is the output of the code below? class Test { public static void main(String[] args) { Teacher yourTA = new TeachingAssistant(); yourTA.grade(); yourTA.officeHour(); } } abstract class Teacher { public Teacher() { System.out.println("New Teacher created"); } abstract void grade(); void officeHour() { System.out.println("Office Hours"); } } class TeachingAssistant extends Teacher { @Override void grade() { System.out.println("Grade your hw7 now"); } } A. "Grade your hw7 now"; "Office Hours" B. "New Teacher created"; "Office Hours" C. "New Teacher created"; "Grade your hw7 now"; "Office Hours" D. An error message will be raised.

C. "New Teacher created"; "Grade your hw7 now"; "Office Hours"

What will be the output of the following program? class A { public void display() { System.out.print("One"); } } class B extends A { @Override public void display() { super.display(); System.out.print("Two"); } } public class C { public static void main(String args[]) { B screen = new B(); screen.display(); } } A. "One" B. "Two" C. "OneTwo" D. "TwoOne"

C. "OneTwo"

Which of the following is correct to create an ArrayList in Java? A. ArrayList<int> myList = new ArrayList<int>(); B. ArrayList<Integer> myList = new int[10]; C. ArrayList<Double> myList = new ArrayList<Double>();

C. ArrayList<Double> myList = new ArrayList<Double>();

Which of the following declarations is correct for a list that is expected to contain integers? A. ArrayList<String> list = new ArrayList<String>(); B. ArrayList<int> list = new ArrayList<int>(); C. ArrayList<Integer> list = new ArrayList<Integer>();

C. ArrayList<Integer> list = new ArrayList<Integer>();

What kinds of data type(s) can we store in an array? A. Primitive values B. Objects C. Both of the above

C. Both of the above

What class does readLine() belong to? A. Reader B. InputStreamReader C. BufferedReader D. FileReader

C. BufferedReader

What is the output of the following code? class A { protected void display() { System.out.print("One"); } } public class B extends A { void display() { System.out.print("Two"); } public static void main(String[] args) { B b = new B(); b.display(); } } A. "One" B. "Two" C. Compile error D. None of the above

C. Compile error

What restriction is there on using the super reference in a constructor? A. Only one child class can use it. B. It can only be used in the parent's constructor. C. It must be used in the first statement of the constructor. D. It must be used in the last statement of the constructor.

C. It must be used in the first statement of the constructor.

What will happen when the following test is running? assertThrows(IndexOutOfBoundsException.class, () -> { // ArrayLists are like arrays in Python ArrayList<String> myList = new ArrayList<String>(); // return the first element of the list String firstString = myList.get(0); }); A. Throw an error B. Fail C. Pass

C. Pass

When using Eclipse's debugger, which button will you click if you want to go back to the caller of the current method? A. Step Into B. Step Over C. Step Return D. Resume

C. Step Return

How can we stop execution of a program and move from one desired step to another? A. logger.error B. System.out.println C. breakpoints D. catch

C. breakpoints

Which of the following is the correct syntax for defining a new class A based on the superclass B? A. class A uses B B. class A implements B C. class A extends B D. class A inherits B

C. class A extends B

Which type of variable or method can only be accessed from within the current package? A. public B. protected C. default (no access modifier) D. private

C. default (no access modifier)

Which way is correct to add the element a to the last position of the ArrayList myList? A.myList.append(a); B. myList.addLast(a); C. myList.add(a);

C. myList.add(a);

Which of the following declares a class correctly? A. public CIT { } B. public class CIT ( ) C. public class CIT { } D. Public Class CIT { }

C. public class CIT {}

What is used in Java to surround code blocks? A. : B. Indentation C. {} D. ()

C. {}

What is the usage of the java keyword 'super'? A. 'super()' can be used to invoke a parent class constructor B. 'super' can be used to refer to parent class instance variables C. 'super' can be used to invoke a parent class method D. All above are true

D. All above are true

What is the output for the following code? class A { private int number; public String name; private void display() { System.out.print("One"); } public String printname(String name) { this.name = name; System.out.print(name); return name; } } public class B { public static void main(String[] args) { A a = new A(); a.display(); a.printname("Aa"); } } A. "Aa" B. "One" C. "OneAa" D. Compile error

D. Compile error

What will be the output of the following program? abstract class A { public void display(); } class B extends A { @Override public void display() { System.out.print("Two"); } public void reset() { System.out.print("Zero"); } } public class C { public static void main(String args[]) { A screen = new B(); screen.display(); screen.reset(); } } A. "OneTwoZero" B. "OneZero" C. "TwoZero" D. Compile error

D. Compile error

What is the output of the provided code if we change the first line in the main function as follows? Beverage bev = new Beverage(); A. "Pouring Tea" B. "Pouring Coffee" C. Nothing will be printed out. D. It will raise an error.

D. It will raise an error.

Let us say there are three classes: Laptop, MacBook, and ThinkPad. What are the likely relationships between these classes? A. ThinkPad is the superclass, MacBook and Laptop are subclasses of ThinkPad. B. There are no relationships between them. C. Laptop is a superclass, MacBook is a subclass of Laptop, and ThinkPad is a subclass of MacBook. D. Laptop is the superclass, MacBook and ThinkPad are subclasses of Laptop.

D. Laptop is the superclass, MacBook and ThinkPad are subclasses of Laptop.

What type of collection would we use if we wanted to eliminate duplicates? A. List B. Map C. Queue D. Set

D. Set


Ensembles d'études connexes

Nurs 200 Midterm Chaminade University

View Set

English 121 - Chap. 48, 49, 50, 51, 52, 53

View Set

Business Law - Chapter 24 LearnSmart

View Set

Chapter 13 Cyber Risk, Terrorism, International Insurance

View Set

Pharmacology ATI Capstone Pre Test

View Set