CNIT 255 Exam 1 (No syntax)

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What Java expression can you write to tell if x is not smaller than 2?

(x >= 2)

What is printed? int x = 0; x += x++; System.out.println(x);

0

What is printed? System.out.println("12" + "13");

1213

if "class X extends Y", which is most true?

A

What keyword do you put in front of ints and doubles to make them constants?

Final

What are related classes grouped into, in Java?

Packages

Here's a crazy for loop. What does it print? for(boolean b = true; b; b = !b) { System.out.println(b); }

True

What method should you override in a subclass of JComponent to do custom painting?

paintComponent(Graphics g)

What happens when the computer goes to run this line of code? System.out.println("String".substring(2, 6));

ring

reads input from the console (or from a String)

scanner class

What is the difference between the variables declared on these two lines: int x; int y[];

single integer; array

In the example above: A) class A is the ____class of class B. B) class B is a ____class of class A. C) a.compute is an example of ____ binding.

superclass, subclass, dynamic

True or false: even if not in the .java file, a class always has at least one constructor. ❏ true ❏ false

true (the default "no-argument" constructor is always implied if others are not present)

Write a short snippet of code to remove the spaces from the beginning and end of the following AND convert what's left to all lower case: String s = " NeAtJaVa ";

(s = s.trim().tolowerCase(); // or similar)

Compare and contrast the this and super keywords

(this is a magic variable referring to the current object within an instance method, and can be used to disambiguate variables with the same name. super is a reference to the super class.)

Which fields can a subclass access from a superclass? ❏ public ❏ private ❏ protected

1st and 3rd

Mark all that are true: ❏ In Java, "Global Data" is allowed to exist outside of a class. ❏ Any variable that isn't a "primitive value" is an object. ❏ An "instance" of a class C is an object of type C. ❏ OOP stands for "Object-Oriented Procedures."

2nd and 3rd

Mark all that are true of constructors: ❏ Calling a constructor creates a new class. ❏ 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.

2nd, 3rd, 4th

How can you make sure you're overriding a method rather than just accidentally introducing a new method?

@Override

How can you print the String "hello" in Java? A) System.out.println("hello"); B) printf("hello"); C) "hello".print(); D) println("hello"):

A

How do you find the length of an array "arr"? A) arr.length B) arr.length() C) arr.size D) arr[-1] E) System.array.length(arr);

A

What best describes the "String [] args" in a main() method? A) It allows someone to pass arguments into your program when they run your code. B) It explains to the compiler that your program is to be run from the command line instead of double clicked from the desktop. C) It defines a variable called String of type args. D) It's not typed right and will cause a compile-time error.

A

Which describes Java? A) Object Oriented B) Architecture-Neutral C) Multithreaded D) All of the above.

A

Which conversions are "automatic"? List all that apply: A) int => double B) double => int C) char => int D) long => int E) double => short

A and C

Which of the following statements is correct? A final class cannot be public. A public class cannot be final. A final class cannot be extended. final is not a valid class modifier.

A final class cannot be extended.

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. ❏ Use descriptive names for methods, fields, and classes. ❏ Break up classes with too many responsibilities into two or more other classes.

All apply

How can you make a "true copy" of an array?

Arrays.copyOf()

static fields: A) Are methods that are called when a program is first created. B) Exist once per class, and represent a single "shared" variable no matter how many instances of a class exist. C) Are constant. D) Are dangerous sources of electro-static discharge

B

Which is true of the char data type? (list all that apply) A) It is considered one of the four "integer" variable types B) It represents a unicode character by mapping a numerical value to a glyph. C) The maximum numerical value it can hold is 255. D) char literal values are typed in java programs using double quotes (e.g. "A") E) Some char literal values need a slash (\) in front of them.

B and E

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

Both A and B.

How do you get the number of elements in an ArrayList called list? A) list.size B) list.length C) list.size() D) list.length()

C

What best describes a String? A) It is a primitive variable type in Java B) One can be created by putting it in single quotes, 'like this' C) Strings are sequences of unicode characters. D) You can only call a method on a String if you put it in a variable first.

C

What does it mean for a method to be "overloaded"? A) It's trying to do too much. B) It's being called with too many parameters. C) There are two or more versions of the method, differentiated in the number, order, or type of parameters they take. D) The method has been redefined in a different class.

C

What is true of a static method? A) It has no this variable while it is executing. B) Within its containing class, it can only access static fields. C) All of the above.

C

What will the value of x be after the following expression is evaluated? int x = 14 % 10 + 5 / 4; A) 5.25 B) 2.65 C) 5 D) 2

C

Which describes the origins of Java? A) Java was invented in in 2000. B) Java has always been used mostly at the server level, since it could not compete with better options for dynamic content in the browser. C) Java was considered reasonably safe and secure, running in a limited "sandbox." D) Oracle, who invented Java, made sure Netscape, Internet Explorer, and the other major browsers at the time had a unified version of Java built into them.

C

Which of the following isn't true in Swing: A) Create a frame by using extends JFrame B) Make closing the window end the program by using frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); C) Show the frame on the screen by calling frame.makeVisible(true); D) Be sure to use EventQueue.invokeLater() when executing code that affects GUI appearance.

C

What are some techniques for naming initialization parameters in a constructor that you want to copy to an instance field? A) Name the initialization parameter as the first letter in the field name. B) Add a dummy prefix to the initialization parameter, i.e., name becomes aName. C) Make use of this inside the constructor, and name the parameter the same as the field. D) All of the above.

D

Which is guaranteed, of String s = "abc".substring(0, 0); A) It will prevent the file it's in from compiling B) s == null is guaranteed to evaluate true C) s == "" is guaranteed to evaluate true D) "".equals(s) is guaranteed to evaluate true E) s has non-zero length F) it will crash the program when it goes to run

D

Why might you ever use a do/while loop instead of a regular while loop? A) You wouldn't - do/while isn't a valid Java construct. B) It lets you define variables used in the loop conditional that go away when the loop is done. C) It saves the value of the loop expression and checks that only after running the loop. D) It guarantees that the loop will run at least once.

D

What is returned by Math.pow(3,2) A) "bang!bang!bang! bang!bang!bang!" B) 8 C) 9 D) 8.0 E) 9.0

E

True or false - when using add(), you can only add an element at the end of an ArrayList. ❏ true ❏ false

False

True or false: two methods with the same name and parameters can co-exist in the same class if they have a different return type. ❏ true ❏ false

False

Name as many floating number types in Java as you know:

Float and Double

It is a message generated when "something interesting" happens, such as a button being clicked or a window closed; and also the act of a response being triggered for that "something", in the form of a method being called with that message.

GUI Event

GUI stands for

Graphical User Interface

What's a good layout manager to produce the following?

GridLayout

Give two examples of layouts in Swing.

GridLayout, FlowLayout

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

JComponent

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

JFrame

allows us to have different methods with the same name, but different parameters (number of parameters, order of parameters, and/or data types of parameters).

Method overloading

Name + parameter types = ???

Method signature

Suppose you have a correctly written java file, "MyClass.java". When it is compiled, what new file will be produced?

Myclass.class

As opposed to declaring an array, what keyword actually creates an array?

New

Can an abstract class be instantiated?

No

Why does it make sense that the main() method must be static?

No object exists yet upon which we can call a regular method, when a program first starts

In your own words, how can and how can't a method alter variables passed by the calling function?

Primitive variables can never be altered by calling a function on them. Objects can not be reassigned by calling a method on them, but just as final objects can have their state changed, so can objects passed as parameters to methods be altered.

can only be accessed from within the class.

Private instance variables

can be accessed from anywhere

Public instance variables

The ____ keyword is used to invoke a superclass constructor.

SUPER

Write a little Java to read the next integer from System.in:

Scanner in = new Scanner(System.in); int x = in.nextInt();

can be used anywhere by referring to the class

Static fields/functions

A JPanel is an invisible container that can hold components. True or False?

True

True or false - the first element in an ArrayList called list, assuming it contains elements, is list.get(0). ❏ true ❏ false

True

True or false: all classes belong to a package of some kind. ❏ true ❏ false

True

True or false: two classes are allowed to have the exact same name as long as they are in different packages. ❏ true ❏ false

True

True or false: you are allowed to change the state of a final variable (e.g., the "name" or "salary" instance field of an Employee variable). ❏ true ❏ false

True

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

View, Controller, Model

When might you prefer an ArrayList over an Array?

When you don't know how many elements you're going to need ahead of time. Or you want to make use of add() and remove()

Name 3 differences between Array and ArrayList in Java.

a) Array has a fixed size. ArrayList has a dynamic size. b) Array can contain both primitives and objects. ArrayList can only contain objects. c) Array members are accessed using []. ArrayList has methods.

How is a constructor different from other methods?

a) Constructors initialize objects being created with the new keyword. b) Constructors are not called directly. They are called implicitly when using new. Methods can be called directly on an object that has already been created. c) Constructors must have the same name as the class name. Constructors cannot return anything (implicitly, the object itself is returned). Methods must declare the return value, even if it is just void. d) Constructors and methods both can have parameters, modifiers (public, private, etc.), and method bodies in braces.

Assume we have two Integer objects, num1 and num2. a) How can we test if num1 is equal to num2? b) How can we test if num1 is smaller than num2? (Hint: Look at the Integer class in the Java API)

a) num1.equals(num2) b) There are multiple ways. First off, you can call intValue() on each Integer object, then compare the two resulting ints as normal: num1.intValue() < num2.intValue(); OR you can use compareTo: num1.compareTo(num2) < 0; OR you can simply compare them as if they were regular ints: num1 < num2. The last one works because the comparison operator "<" is not defined for objects, and both Integer objects will automatically be compared as int.

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. a. Below -10°C --> 100%. b. Between -10°C and 10°C --> 80%. c. Between 10°C and 25°C --> 60%. d. 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.

is a function that has no implementation. We use it when we want our subclasses to provide the implementation for it.

abstract function

In your own words, what's the difference between break and continue?

continue skips the rest of the current iteration of a loop; break skips the rest of the loop altogether

It means it looks and behaves the same on each operating system. Because Swing components are painted "from scratch", you can implement a "cross-platform look", and write a single GUI that works and looks exactly the same way on every operating system

cross-platform look

What Object-Oriented Programming (OOP) principle does the keyword private facilitate?

encapsulation

True or false: if you try to cast to an invalid type the result is null. ❏ true ❏ false

false (either the compiler won't even let you run the program, or you'll get a ClassCastException)

True or false: all instance fields of an object are initialized to null if you don't assign to them explicitly. ❏ true ❏ false

false (this is correct for object variables, but primitives become 0/false)

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

import java.time.*;

What is a benefit of marking variables "private"?

it forces access to variables to be encapsulated in getters and setters

only accessible through actual objects.

non-static fields

What's "strange" about Math.sqrt()?

you don't need sqrt()


संबंधित स्टडी सेट्स

Network+ Guide to Networks CompTIA Chapter 2

View Set

W4P3 Alterations in Renal Function

View Set

automotive chapter definitions Engine

View Set