CSE 1321 Midterm (Modules 1-4)_SHARED.Version_

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

Comparison/Relational Operators are:

"==" equals to "!=" not equals to ">" greater than "<" less than ">=" greater than or equal to "<=" less than or equal to

logical operators are:

(AND(&&) OR(||) NOT(!))

while loop

(conditional statement) [if it's true] Execute all the code in between curly braces 1. It reassess at top of loop to see if the condition is still true 2. Will run 0 to infinite number of times 3. indeterminate Use a while loop is useful when you don't know how many times something will repeat; the loop could "go on forever". As an example, if you ask a user to enter a number between 1- 10 and they consistently enter 45, this could go on forever. Eventually (and hopefully), the user would enter a valid number.

a variable is

- a chunk of the computers memory - can hold a value - int, float, char, string

what is an algorithm?

- a set of logical steps to accomplish a specific task - a set of directions/instructions -data and instructipns that work on data

a string is actually:

- composite of other data types - Strings are immutable which means once a String object is created its contents cannot be changed. (cant change in memory) - complex: can be broken down into characters

what are 6 properties of a good algorithm?

- precise - unambiguous - complete - correct - simple - contain levels of abstraction

what does 2 % 5 equal:

0 remainder 2

int main () { int x = -1; if ((x*2) ==4) { cout<<x; } else if (x==1) { cout << "End loop" << endl; } else if ((x%5)==0) { cout << "Boom!" << endl; } else { cout << x*x << endI; } return 0; }

1

skeleton program for 1) C# 2) C++ 3) Java

1) using System; class Program { public static void Main (string[] args) { } } 2) #include <iostream> int main() { return 0; } 3) class Main { public static void main(String[] args) { } }

print "hello world" in 1- C# 2- Java 3- C++

1- Console.WriteLine("hello world"); 2- System.out.println("hello world"); 2- cout << "hello world" << endl;

what is the order of precedence for operators

1- arithmetic operators (*, /, %) (+, -) 2- comparison/relational operators (<, <=, >, >=, ==, !=) 3- logical operators (!) (&&) (| |)

5 components of algorithms

1- data structures 2- instructions 3- conditional expressions 4- control structures 5- modules

What are the three parts of all loops

1- initial condition (where you want the loop to start) 2- ending condition (where the loop will hit false and stop so it doesnt go on forever) 3- what youre incrementing your counter by (incriment statement block) (i++, i--) ++ = increment, adding 1 each time -- = decrement, subtracting 1 each time.

do - while loop

1. Will run at least once 2. Do this line of code while it's true we loop 3. indeterminate Do {<insert code here>} while (loop - continuation- conditional); Use a do-while loop when the loop must execute at least one time. The loops above can execute 0 times, but not this one! The reason is because, for all loops, there is a test to see if the loop should continue repeating. With a do-while loop, that test is at the bottom.

10 +=10 is a shortcut operator for:

10+10 =

Evaluate as an integer: 5% 7 + 3 * 2 + 4

14

Evaluate 10 - (40 + (20 / (10 - 5) * 2) / 4) + 50 Assume all values are integers.

18

Evaluate 10 - (40 + (20 / (10 - 5) * 2) / 4) + 50 Assume all values are integers.

18 ( %, /, *) Solve left to right

int y= 5; for (int count = 0; count < 7: count++) { y += count; cout<<y<<","; if (count ==5) { continue; } } cout << endl; }

5, 6, 8, 11, 15, 20, 26,

Evaluate as an integer: 5 + 3 / 2 * 4 - 2% 1

9

What is the shortcut operator to add one (1) to a variable? A) ++ B) + + C) ** D) @@

A) ++

Mathematical Operators are:

Addition: + subtraction: - Multiplication: * Divison: / Modulus (remainder): %

Program design consists of: A) the ability to solve problems. B) steps a programmer should do before they start coding a program in a specific language. C) writing the code for a program. D) writing the documentation for a program.

B) steps a programmer should do before they start coding a program in a specific language.

List the eight simple or primitive data types and their range of values: 1) 2) 3) 4) 5) 6) 7) 8)

Be Careful, Bears Shouldn't Ingest Large Furry Dogs 1) byte, 2) short, 3) int, 4) long, 5) float, (7 signifigant digits) 6) double, (14 signifigant digits) 7) boolean, (2 values, true and false) 8) char

Pseudocode is A) a formal programming language. B) a machine. C) an informal high-level description of the operating principle of a computer program or algorithm.

C) an informal high-level description of the operating principle of a computer program or algorithm.

This statement skips an iteration of a loop

CONTINUE

What is the Basic Program Structure for any language?

Class Name Main Method Input/Read Statement, Output/Write Statement Assignment Statement

Which of these is not a property of a good algorithm? A) Unambiguous B) Complete C) Precise D) Complex

D) Complex

What is the best way to tackle a coding problem?

IMPO Input - what info do you need from the user? Memory - What do I need to store? Processing - What calculations do I need? Output - What will I display to the user?

What is the order of operations:

P - parenthesis E - exponents (2^2) M - multiplication D - division (modulus before division) A - addition S - subtraction

Postfix Increment

Postfix increment says to use my existing value then when you are done with the other operators; increment me. Int oldest = 44; Age = oldest ++;

Prefix Increment

Prefix increment says to increment me now and use my new value in any calculation. Int oldest =44; Age = ++oldest;

Consider two variables x and y. If the values of x =5 and y=10 BEGIN MAIN IF(x < 0) { PRINT "Mr.Spock" } ELSE { IF (x > y) { PRINT "Captain Kirk" } ELSE { PRINT "Star Trek it is!" } } END MAIN What is displayed as the result of the code is being executed? a) Nothing will be printed/displayed b) Captain Kirk c) Star Trek it is! d) Mr. Spock

c) Star Trek it is!

Which statement below will be true after the following code terminates? BEGIN MAIN CREATE x = 0 WHILE (x < 100) { x *= 2; } END WHILE END MAIN a) x == 98 b) x == 2 c) The loop won't terminate. It's an infinite loop. d) x == 0

c) The loop won't terminate. It's an infinite loop. (0 * 2 will always be 0 so infinite loop)

Programming starts with: a) looking online for the answers. b) writing code until it works. c) developing an algorithm.

c) developing an algorithm.

What is the output of the following code? BEGIN MAIN int u = 3; int v = 5; u += v; v += u; u -= v; v -= u; PRINT (u + ", " + v); END MAIN a) 5, 3 b) -5,-3 c) 3, 5 d) -5, 18

d) -5, 18 EXPLANATION 3 += 5 = 8(u) (3 + 5) 5 += 8 = 13(v) (5 + 8) 8 -= 13 = -5(u) (8 - 13) 13 -= -5 = 18(v) (13 - (-5)) so = -5, 18 is the final outcome

What is the value of j after this code is executed? BEGIN MAIN int i = 6, int j=10; j+=i; END MAIN a) 10 b) 6 c) 4 d) 16

d) 16

What is the output of the following code? BEGIN MAIN int num1 = 500; int num2 = 200; int num3 = 300; double average = num1 + num2 + num3 / 3; PRINTLINE(average); END MAIN a) 333.33333333333 b) 333.0 c) Error message d) 800.0

d) 800.0

Imagine a game of Yahtzee where a roll of dice always returns an even number. Consider this code where the number is variable that stores the value of the dice. BEGIN MAIN CREATE result = number % 2; IF (result == 0) PRINT "TRUE" ELSE PRINT "FALSE" END MAIN What will the code display? a) FALSE b) Unable to determine c) Neither condition is satisfied d) TRUE

d) TRUE

Debugging is the process of a) compiling the source code. b) publishing the source code to the client. c) writing source code. d) solving errors in the source code.

d) solving errors in the source code.

What is white space?

empty area and ignored in program but enhances readability

A String (or string) object is a primitive data type. true or false

false

If x = 3, y = 1, and z = 5 then the following Boolean expression evaluates to true: ( x > 0) && (y == 0) || (z < 5) true or false

false

In programming && is considered an arithmetic operator. true or false

false

Input is sending messages to the console/user. true or false

false

Logical operators are evaluated before relational operators. true or false

false

Output is process of reading information from user, usually via keyboard or mouse. true or false

false

Pseudocode form of writing should be used only when the logic of the program is complex. true or false

false

Strings are a primitive data type. true or false

false

Unary means: What is an example

it is an operator that performs an action on a single operand. (*, -, !, =,

The execution of one of a loop is referred to as an

iteration

output is actually:

messages/ what is being displayed to the user based off of the input information

input is actually:

reading and taking in information from the user

What is concatenation?

the joining of two things together in a println (or writeline) - ex: system.out.prinln("hello " + answer); - ex: Console.WriteLine("hello " + answer);

A flowchart is a type of diagram that represents an algorithm, workflow or process. true or false

true

A relational operator used when comparing primitive data types is !=. true or false

true

A switch statement is similar to an if statement in that both are used to alter the flow of a program. true or false

true

An if statement does not need to have an else clause. true or false

true

If the expression xyz % 3 == 0 is true and xyz is a positive integer, then the value stored in the variable xyz is evenly divisible by 3. true or false

true

If the variable isTested is a Boolean, then the statement below is a valid control expression: IF(isTested) true or false

true

In general, the advantage of using a binary search over a linear search increases when searching larger sorted arrays. True False

true

In programming, ! is considered a unary, relational operator.

true

Is this valid syntax for a FOR loop? BEGIN MAIN FOR(CREATE j = 0; j < 1000; j++) PRINT(j) END FOR END MAIN True or False

true

Software testing involves the execution of a software component or system component to evaluate one or more properties of interest. True or False

true

The MAIN method tells the program where to begin running code as it is the entry or starting point for the program. true or false

true

When assigning a value to a variable, the variable must be on the left of the assignment operator (=). true or false

true

count++ is equivalent to count = count + 1. true or false

true

3 types of loops:

while do... while for

A DEFAULT CASE is required as part of a SWITCH statement.

False

True or False: The following is an example of a valid program in Pseudocode: END MAIN

False

Whitespace has an important role in the functionality of a program in C++, Java, C# and Pseudocode.

False

You must declare and initialize a variable in two separate steps.

False

import java.util.Scanner, class Main public static void main(String[] args) Scanner input = new Scanner(System.in); int selection = 0; String output =", while (sclection!=-]) System.out-print ("Please enter a number to convert (-I to stop): "): sclection = input.nextintO; kwitch(sclection) case -I: output += ("InHave a good day.");break; case 1: output += "One ", break; case 2: output += "Two ": break; casc 3: output += "Three: break; case 4: output += "Four": break; case 5: oulput += "Five "; break; case 6: oulput += "Six ": break; case 7: output += "Seven ": break: case 8: output += "Eight ", break; case 9: output += "Nine " break; case 0: output += "Zero "; break; default: System.out. printin("Invalid entry."); break; if (output != "*) f System.out. printin("Final output is: " + output); input.closcO: What is the exact output if a user enters: 4 7 8 -1?

Final output is: Four Seven Eight Have a good day.

What data types can a switch statement use?

Int type Char type string type - in java or C#

What is the Skeleton Program

It defines the entry/starting point. Smallest program you can write - it does nothing. Begin Main End Main

What is abstraction?

It is reducing information and detail to focus on essential characteristics. (logical grouping of concepts pr objects)

What type of error is present in the underlined C# code sample below? public static void Main(string[] args) { Console. WriteLine("Enter a number"); int num = Convert.Tolnt32(Console. ReadLine0)); int squared = num + num; Console. WriteLine/num+ "squared is "+ squared); }

Logic Error

for loop

Loops that have a predetermined beginning, end, and increment (step interval). run explicitly the number of times - the number is specified or determinant for (block; block; block) { } for (at the beginning of the loop, do this; stop the loop if this is true; do this after each 'cycle' of the loop) Use a for loop when you want to repeat something a certain number of times. For example, if you want to repeat something 100 times, and a for loop is a good candidate for that. Or, if you wanted to count from 50 to 3000 in increments of 10, you could do that too.

limport java.util.Scanner; Class Main public static void main (String args(]) Scanner input = new Scanner(System.in); System.out print ("Please enter your initials: String initials = input.nexiO: System.out, printin ("Your initials: ' " + initials): input.closcO: Which line declares and initializes a variable named initials?

String initials = input.nextf;

What type of error is present in the underlined Java code sample below? public static void main(String[) args) { Add 1 + 1 and print the results int result = 1 + 1; System.out.printin(result): }

Syntax

What type of error is present in the underlined C++ code sample below? using namespace std; int main() { dble value = 12; cout «< value «< endl: }

Syntax Error

class Main public statie void main(String!] args) Scanner input = new Scanner(System.in); it hoursCompleted, hours Remaining; in TOTALHOURSFORDEGREE = 120; System.out.print("Please enter number of credit hours completed: "); hoursCompleted=input.nextintO; hoursRemaining = TOTALHOURSFORDEGREE - hoursCompleted; System.out.print("You still need to complete " + hoursRemaining + " hours) to complete your degree."); input.closcO; Which line asks for the user's input?

System.out.print("Please enter number of credit hours completed: ");

what would be stored if the user enters 5? using system; class Mainclass { public state void Main (string[] args) { int hoursRegistered, difference; in FULLTIME = 12; Console.Write("please enter the number of credit hours you have registered for: "); hoursRegistered = Conver.ToInt16(Console.ReadLine()); difference = FULLTIME - hoursRegistered; Console.WriteLine("You need to register for " +difference+ " more hour(s) to be considered full-time."); } } a) 15 b) 2 c) 7

c) 7

Abstraction refers to the logical grouping of concepts or objects.

True

Consider this code: BEGIN MAIN int y=1; int x=1; PRINTLINE(x++); END MAIN What will this code print? a) 2 b) 0 c) 1 d) unknown

c) 1

import.java.util.Scanner; class Main { public static void main(String[] args) { int z = 10; for(int count = 10; count > 5; count--) { if(count % 2 == 0) { z += count; } System.out.print(z + ","); } } What is the exact output for this code? a) 20, 20, 28, 34 b) 20, 24, 28, 20, 24 c) 10, 9, 8, 7, 6

a) 20, 20, 28, 34

5*7+3%2-4 equals a) 32 b) 18 c) 22

a) 32 because 5*7 = 35 3%2 = 1 35+1-4 36-4 = 32

using namespace std; int main() { in choice; cout << "please make your selection (1-6): "; cin >> choice; switch (choice) { case 1: cout << "Go Atlanta Braves!"; break; case 2: cout << "Go Atlanta Dream!"; break; case 3: cout << "Go Atlanta Falcons!"; break; case 4: cout << "Go Atlanta Gladiators!"; break; case 5: cout << "Go Atlanta Hawks!"; break; case 6: cout << "Go Atlanta United!"; break; default: cout << "Invalid selection."; break; } } what would be displayed if the user selected 1: a) Go Atlanta Braves! b) Go Atlanta Dream! c) Invalid Selection.

a) Go Atlanta Braves!

convert the following if statement into a logically equivalent SWITCH statement: IF (X == 12) THEN PRINT "Hola!" ELSE IF (X == 14) THEN PRINT "Hello!" ELSE PRINT "Chao!" END IF a) SWITCH X CASE 12: PRINT "Hola!" BREAK CASE 14: PRINT "Hello!" BREAK DEFAULT: PRINT "Chao!" END SWITCH b) SWITCH X CASE 12: PRINT "Hola!" CASE 14: PRINT "Hello!" DAFAULT: PRINT "Chao!" END SWITCH c) SWITCH X CASE 12: PRINT "Hola!" BREAK CASE 14: PRINT "Hello!" DEFAULT: PRINT "Chao!" END SWITCH

a) SWITCH X CASE 12: PRINT "Hola!" BREAK CASE 14: PRINT "Hello!" BREAK DEFAULT: PRINT "Chao!" END SWITCH

which of the following variables could hold the literal numeric value 3.141 without losing any information a) float x b) int y c) char z d) string w

a) float x

Consider the expression: value >= 30 Which of the following is equivalent to this expression? a) value > 30 OR value == 30 b) NOT(value < 29) c) NOT(value > 31) d) value > 30 AND value == 30

a) value > 30 OR value == 30

8-3*2/1+2 equals a) 0 b) 4 c) 8 d) -2

b) 4 because: 3*2=6 8-6/1+2 8-6+2 2+2 = 4 according to PEMDAS it would be 0 but the test wanted you to use left to right math in the last part

IDE stands for a) Initial Degree Expectations b) Integrated Development Environment c) Intramural Department Executive

b) Integrated Development Environment

The Boolean expression ((A AND B) AND (NOT(A AND B)) evaluates to: a) true whenever both A is true and B is true. b) false in all cases. c) true whenever only A is true or only B is true. d) true in all cases.

b) false in all cases.

What would be the best datatype to represent product? A variable that stores information about the number of items currently in stock in a grocery store. a) double b) int c) float d) String

b) int (number of items in the store is going to be a whole number)


Set pelajaran terkait

HIS 141 - Age of Exploration Reformations

View Set

Ch. 16: Working with the Command-Line Interface

View Set

Chapter 5 Study Guide (From Quiz)

View Set