cs 152 computer programming fundamentals quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · cs 152...

48
CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico Spring 2020

Upload: others

Post on 15-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

CS 152Computer Programming

FundamentalsQuiz 7

Brooke Chenoweth

University of New Mexico

Spring 2020

Page 2: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 1Which is the best variable declaration for thenumber of people in your family?

A boolean foo;

B boolean familyMemberCount = 1;

C int familyMemberCount = 1;

D float familyMemberCount = 1;

E double familyMemberCount = 1;

Page 3: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 1Which is the best variable declaration for thenumber of people in your family?

A boolean foo;

B boolean familyMemberCount = 1;

C int familyMemberCount = 1;

D float familyMemberCount = 1;

E double familyMemberCount = 1;

Page 4: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 2

public class TestClass {

public static void main(String [] args) {

int x = 5;

int y = 10;

y = y - x / 2;

System.out.println(y);

}

}

8

Page 5: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 2

public class TestClass {

public static void main(String [] args) {

int x = 5;

int y = 10;

y = y - x / 2;

System.out.println(y);

}

}

8

Page 6: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 92

a 3

b 1

Page 7: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 92

a 3

b 1

Page 8: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 17

a 3

b 1

Page 9: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 17

a 3

b 1

Page 10: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 7

a 3

b 1

Page 11: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 3

public static void main(String [] args) {

int n = 92;

int a = n / 25;

n = n % 25;

int b=n / 10;

n = n % 10;

System.out.println(a);

System.out.println(b);

System.out.println(n);

}

n 7

a 3

b 1

3

1

7

Page 12: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 4Which of the following is not part of all loops?

A initialization

B loop body

C termination condition

D the keyword “while”

Page 13: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 4Which of the following is not part of all loops?

A initialization

B loop body

C termination condition

D the keyword “while”

Page 14: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8, 7, 6, 4, 3,

Page 15: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9,

8, 7, 6, 4, 3,

Page 16: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8,

7, 6, 4, 3,

Page 17: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8, 7,

6, 4, 3,

Page 18: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8, 7, 6,

4, 3,

Page 19: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8, 7, 6, 4,

3,

Page 20: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 5What is the output of the following code?

public static void main(String [] args) {

int n = 10;

int z = n-1;

while (z > 1) {

if ((n % z) != 0) {

System.out.print(z +", ");

}

z--;

}

}

9, 8, 7, 6, 4, 3,

Page 21: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 6What is the output of the following code?

public static void main(String [] args) {

int a = 4;

for (int i=a*a; i > 1; i -= 5) {

System.out.print("(" + i + "," + a + ") ");

}

System.out.println ();

}

(16,4) (11,4) (6,4)

Page 22: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 6What is the output of the following code?

public static void main(String [] args) {

int a = 4;

for (int i=a*a; i > 1; i -= 5) {

System.out.print("(" + i + "," + a + ") ");

}

System.out.println ();

}

(16,4)

(11,4) (6,4)

Page 23: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 6What is the output of the following code?

public static void main(String [] args) {

int a = 4;

for (int i=a*a; i > 1; i -= 5) {

System.out.print("(" + i + "," + a + ") ");

}

System.out.println ();

}

(16,4) (11,4)

(6,4)

Page 24: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 6What is the output of the following code?

public static void main(String [] args) {

int a = 4;

for (int i=a*a; i > 1; i -= 5) {

System.out.print("(" + i + "," + a + ") ");

}

System.out.println ();

}

(16,4) (11,4) (6,4)

Page 25: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The value of the square root of 3

• boolean

• double

• int

• Random

• Scanner

• String

Page 26: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The value of the square root of 3

• boolean

• double

• int

• Random

• Scanner

• String

Page 27: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The current state of a light switch (on or off)

• boolean

• double

• int

• Random

• Scanner

• String

Page 28: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The current state of a light switch (on or off)

• boolean

• double

• int

• Random

• Scanner

• String

Page 29: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The number of adults in a household

• boolean

• double

• int

• Random

• Scanner

• String

Page 30: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The number of adults in a household

• boolean

• double

• int

• Random

• Scanner

• String

Page 31: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The result of a coin flip

• boolean

• double

• int

• Random

• Scanner

• String

Page 32: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The result of a coin flip

• boolean

• double

• int

• Random

• Scanner

• String

Page 33: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The title of a movie

• boolean

• double

• int

• Random

• Scanner

• String

Page 34: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 7What is the best type to store the following?

The title of a movie

• boolean

• double

• int

• Random

• Scanner

• String

Page 35: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 8Which code would you use to create an array thatcould hold 50 String objects?

A Strings names = new String(50);

B String names = new String[50];

C String names[50];

D String[50] names;

E String[50] names = new String();

F String[] names = new String;

G String[] names = new String[50];

H String[] names = new String(50);

I String[] names = new [String](50);

Page 36: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 8Which code would you use to create an array thatcould hold 50 String objects?

A Strings names = new String(50);

B String names = new String[50];

C String names[50];

D String[50] names;

E String[50] names = new String();

F String[] names = new String;

G String[] names = new String[50];

H String[] names = new String(50);

I String[] names = new [String](50);

Page 37: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

apple && orange

Page 38: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

apple && orange false

Page 39: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

apple || orange || banana

Page 40: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

apple || orange || banana true

Page 41: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!kiwi

Page 42: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!kiwi true

Page 43: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

(apple || kiwi) && (orange || kiwi)

Page 44: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

(apple || kiwi) && (orange || kiwi) false

Page 45: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!apple && (!orange || banana || kiwi)

Page 46: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!apple && (!orange || banana || kiwi) false

Page 47: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!apple || (!orange || banana || kiwi)

Page 48: CS 152 Computer Programming Fundamentals Quiz 7bchenoweth/cs152/cs152lecture20-quiz7.… · CS 152 Computer Programming Fundamentals Quiz 7 Brooke Chenoweth University of New Mexico

Question 9

boolean apple = true;

boolean orange = false;

boolean banana = true;

boolean kiwi = false;

!apple || (!orange || banana || kiwi) true