CS Ch. 3 Test Review

Ace your homework & exams now with Quizwiz!

how to build a format specifier

% [flags] [.precision] conversion-character

array: length

(size) number of elements in the array

comparing strings

.equals()

comparing strings (ignoring the case)

.equalsIgnoreCase()

does the index of an array start with 0 or 1?

0

what values would the following array contain? int[] nums = new int[5];

0,0,0,0,0

Precedence: Not And Or

1. not 2. and 3. or

Does this work? If so, what does it return? "what".length();

4

ASCII number

48-57

int a = 5; System.out.print(a++); what would this print?

5

Scanner method: specifies split value

Delimiter

expression

a collection of operators and operands that, when evaluated, express some value

parameter

a piece of info a method needs to run

operator

a symbol that represents some operation to be preformed

What is output by the code below? int x = 7; x++; System.out.print(x); a. 8 b. 7 c. 9 d. 10 e. 11

a. 8

public class Dog { private String voice; public void setVoice( String v ) { voice = v; } public void barkOnce() { System.out.print( voice ); } public void barkTwice() { barkOnce(); barkOnce(); } } Given the declaration below, which of the following are valid statements? Dog d = new Dog(); d.setVoice( "roof" ); I. d.barkOnce(); II. d.barkTwice("rrrrrr"); III. System.out.println( d.barkTwice() ); a. I only b. II only c. III only d. I and II only e. none of the these statements are valid

a. I only

What is the output of this code? System.out.print("Insert \\\" Symbols"); a. Insert \" Symbols b. Insert " Symbols c. Insert \\\" Symbols d. Insert \\" Symbols

a. Insert \" Symbols

a container for variables and methods that serves as a blueprint. a. class b. instance c. object d. constructor

a. class

Which of these will not have a return type in its header? a. constructor b. accessor c. mutator d. toString

a. constructor

format specifier conversion characters: c

character [use captial C to uppercase the letter]

class declaration

class Book { // class body }

getting a word from the user

console.next()

getting a boolean from the user

console.nextBoolean():

getting an double from the user

console.nextDouble()

getting an int from the user

console.nextInt()

getting a group of words from the user

console.nextLine()

What is output by the code below? int x = 9, y = 8; int z = x + y; double a = z; System.out.print(a); a. 8 b. 9 c. 17 d. 17.0 e. 27.0

d. 17.0

What is output by the code below? System.out.println( Math.sqrt(81) ); a. 6.0 b. 7.0 c. 8.0 d. 9.0 e. 27.0

d. 9.0

Which of the following Scanner methods can be used to read in the value 32760 ? a. nextShort() b. nextInt() c. nextByte() d. A and B only e. all of these

d. A and B only

Which of the following types uses the least memory? a. int b. double c. float d. char e. long

d. char

Which of these IS NOT a valid return type? a. int b. String c. void d. static

d. static

int [] years = new int [50] what does "int[]" do?

declare the following variable to be an integer array

combining a double and boolean

doesn't work

combining an int and a boolean

doesn't work

combining an int and a double

double

System.out.println( Math.pow(3,3) ); a. 6.0 b. 7.0 c. 8.0 d. 9.0 e. 27.0

e. 27.0

public class Zonk { private int num; public Zonk() { num = 12; } public Zonk(int n) { num = n; } public void add(int n) { num += n; } public String toString() { return "" + num; } } Using the class above, what would you get as output if you ran the code below within a runner? Zonk z1 = new Zonk(7); z1.num = 5; out.println(z1.num); a. 5 b. 7 c. 12 d. 17 e. No output due to a compiler error

e. no ouputdue to a complier error.

in substring (int x, int y) is the int y inclusive or exclusive?

exclusive

true or false? index = .length

false index = .length - 1

format specifier conversion characters: f

floating point number [float, double]

what statement should be used in a for loop if you want to start at 0 and run through the whole array?

for (int i = 0; i < nums.length; i++)

format specifier flags: 0

forces numerical values to be zero-padded (ex. 0015)

Scanner methods: checks to see if there are more Strings

hasNext()

Scanner methods: checks to see if there are more bytes

hasNextByte()

Scanner methods: checks to see if there are more doubles

hasNextDouble()

Scanner methods: checks to see if there are more ints

hasNextInt()

Scanner methods: checks to see if there are more long

hasNextLong()

Scanner methods: checks to see if there are more shorts

hasNextShort()

.length

how many elements a particular array can store

First number of data file

how many pieces of data are in the file

format specifier flags: -

left justify

.equals()

lets us test if a string is equal to a literal or other String

int [] years = new int [50] what does "new int[50]" do?

make a new integer array in the computer's memory that has room for 50 values

mutators/modifiers

methods designed to change instance variables

accessors

methods that don't change anything

! symbol

negation (means not)

instantiation

new Scanner (System.in)

format specifier conversion characters: n

newline [similar to \n]

Scanner methods: returns the next one word String

next()

Scanner methods: returns the next double value

nextDouble()

Scanner methods: returns the next int value

nextInt()

Scanner methods: returns the next multi word String

nextLine()

can an array's size be changed after it's created?

nope

Find object/reference and constructor: Scanner chopper = new Scanner("at it us")

object/reference: chopper constructor: Scanner

Find object/reference and constructor: Scanner keyboard = new Scanner(System.in)

object/reference: keyboard constructor: Scanner

format specifier flags: +

output a plus (+) or minus for numerical values

default constructor

public Cat () { name = ""; breed = ""; }

secondary constructor

public Cat (String n, String b) { name = n; breed = b; }

switch

switch (<variable name>) { case <value>: //code goes here case <value>: //code goes here default: //code goes here }

array: elements

the items (values) stored in an array

index vs. length()

the length() method returns the total number of characters, and therefor starts counting with 1 (not with 0, like an index)

index

the position of a character in a String (starts with 0)

how to declare an array

type [] name ex. int[] years, double[] grades

int [] years = new int [50] what does "years" do?

variable's name

What setup should you use when item count of scanner file is unknown?

while loop

format specifier conversion characters: d

whole number data [byte, short, int, long]

Concatenating Strings

you can add two strings together to create a new string with the + operator or +=

Delimiter example

Scanner chopper = new Scanner("one-two-three"); chopper.useDelimiter("\\-"); while(chopper.hasNext()) { out.println(chopper.next()); }

declare the scanner variable for user input

Scanner console = new Scanner = new Scanner (System.in)

Scanner methods with text file

Scanner file = new Scanner(new File("nums2.txt")) int val = file.nextInt() file connects to nums2.txt

format specifier conversion characters: s

String [use the capital S to uppercase all letters in the string]

what is returned when you are trying to access a character at an index that doesn't exist in the String?

String index out of bounds exception

charAt()

The String method that returns a char in the String at a specified index.

combining a string and a double

string (concatenation)

combining a string and an int

string (concatenation)

int b = 5; System.out.println(++b); what would this print?

6

ASCII uppercase

65-90

("hello" + "hi").length();

7

Does this work? If so, what does it return? String str = "Something"; str.length();

9

ASCII lowercase

97-122

String

An object in Java made up of a sequence or string of characters.

how to print an array

Arrays.toString(array name)

\n

Insert a newline (hit enter) in the text at this point.

raised to a power method

Math.pow(double, double)

square root method

Math.sqrt(double value)

Which of the following lines correctly defines a double variable? a. double d d = 234.1 b. d = 234.1 c. double = 234.1 d. double d = 234.1 e. double = 89.3;

a. double d = 234.1

2. The job of a constructor is to... a. initialize the instance variables b. return data about the object c. make variables private d. make a String version of the object

a. initialize the instance variables

the act of creating an specific instance from a blueprint a. instantiation b. classification c. declaration d. mutation

a. instantiation

a block of code that we can request to run at any time by calling it by name a. method b. variable c. parameter d. loop

a. method

What is output by the code below? public class CS { public void one() { System.out.print("one"); } public void two() { System.out.print("two"); one(); } } //code in the main of another class CS test = new CS(); test.one(); test.two(); test.one(); a. onetwooneone b. oneoneoneone c. twotwotwotwo d. onetwoonetwo e. twooneoneone

a. onetwooneone

Proper encapsulation requires that all instance variables be declared as.... a. private b. public c. static d. final

a. private

What is the output of the code? System.out.print("The Legend\nof\tZelda"); a. The Legend of Zelda c. The Legend \nof\tZelda b. The Legend of Zelda d. The Legend of Zelda

a. the legend of zelda

format specifier precision

always starts with a decimal. number of digits precision when outputting floating point values. numbers are rounded to the specified precision.

array: index

an element's position within an array

What is output by the code below? System.out.printf("%.3f",9.3213); a. 9.3213 b. 9.321 c. 9.32 d. 9.3 e. 27

b. 9.321

What is the ASCII value of 'b'? a. 43 b. 98 c. 48 d. 66 e. 97

b. 98

Which of these has the lowest priority? a. parentheses b. addition c. multiplication d. modulus

b. addiction

Given the following statement, var must be defined as which of the following types? var = keyboard.nextDouble(); a. short b. double c. int d. long e. String

b. double

public class Qwonk { private String word; public Qwonk(String s) { word = s; } public void setWord(String s) { word = s; } public void trimWord(int len) { word = word.substring(0, len); } public String toString() { return word; } } Using the class above, what would you get as output if you ran the code below within a runner? Qwonk fred = new Qwonk("salsa is good!"); fred = new Qwonk("tasty salsa"); fred.trimWord(5); out.println(fred); a. salsa b. tasty c. good! d. alsa

b. tasty

format specifier conversion characters: b

boolean value

What is the operator for modulus? a. // b. # c. % d. &

c. %

What is output by the code below? int x = 9; x = x + 5; System.out.print(x); a. 9 b. 5 c. 14 d. 15 e. 27

c. 14

Identify the mistake in this code: console = System.in; System.out.print("What is your name? >>> "); String name = console.nextLine(); a. The variable x should be declared as double. b. The print statement should be a println. c. The console object isn't instantiated correctly. d. There is no mistake.

c. the console object isn't instantiated correctly

what would you add at the beginning of the code to get user input?

import java.util.*;

Scanning date file import statements

import java.util.Scanner import java.io.File import java.io.IOException

Scanner import statement

import java.util.Scanner;

format specifier flags: ,

includes comma grouping separator for numbers over 999

in substring (int x, int y) is the int x inclusive or exclusive?

inclusive

\\

insert a backslash character in the text at this point

\b

insert a backspace in the text at this point

\"

insert a double quote character in the text at this point

\'

insert a single quote character in the text at this point

\t

insert a tab in the text at this point

combining an int and an int

int

how to bring the array to life

int [] nums = new int[4]

initializing an array to known values

int[] nums = {21, 86, 11, 4, 650}

first line of a main method

public static void main(String[] args)

statement that throws the IOException

public static void main(String[] args)

instance method

public void meow() { System.out.print("meow"); }

operand

quantity or value upon which an operation is being preformed

byte

range: -128 to 127 size: 1 byte

int

range: -2,147,483,648 to 2,147,483, 647 size: 4 byte

short

range: -32,768 to 32,767 size: 2 byte

long

range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 size: 8 byte

char

range: 0 to 65,536 size: 2 byte

double

range: approximately ±1.79769313486231570E+308 (15 significant decimal digits) size: 8 bytes

float

range: approximately ±3.40282347E+38F (6-7 significant decimal digits) size: 4 bytes

boolean

range: true or false size: 1 byte

format specifier width

represents the minimum number of characters to be written in the output

.substring()

returns a smaller portion or a string

.length()

returns the length of a string

String str; indexOf(str)

returns the location of String str in the string, searching from spot 0 to spot length-1

String str; lastIndexOf(str)

returns the location of String str in the string, searching from spot length-1 to spot 0

char ch; indexOf(ch)

returns the location of char ch in the string, searching from spot 0 to spot length -1

char ch; lastIndexOf(ch)

returns the location of char ch in the string, searching from spot length-1 to spot 0

what does: substring(int x) do?

returns the string starting from the int x to the end of the string

substring (int x, int y)

starts at the first int and stops at the second int

combining a string and a boolean

string (concatenation)


Related study sets

3.2.4 Properties of Period 3 elements and their oxides

View Set

10. CEH v11: Vulnerability Assessment, Management & Classification

View Set

Chapter 8 - Hardware Support A+, Chapter 7, Software Support A+

View Set