unit 5 CesSPool

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

Consider the following segment given in pseudo code (see reference from previous question): IF (grade >= 70) { DISPLAY("You passed!") } ELSE { IF(grade <= 70){ DISPLAY("Time to start studying again!") } } What will be displayed if grade is set to 70?

"You passed!"

Consider the JavaScript code segment below. Which statement should be used in place of <missing code> such that the alarm is set to 9:00 am on weekends, and 6:30 am on weekdays. var day = prompt("What day is it tomorrow?"); if ( <missing code> ){ setAlarm = "9:00am"; } else { setAlarm = "6:30am"; }

(day == "Saturday") || (day == "Sunday")

Assume that a variable temperature is assigned like this: var temperature = 30; Choose the two (2) Boolean expressions that evaluate to FALSE. A. (temperature > 0) && (temperature < 32) B. (temperature == 0) || (temperature < 32) C. (temperature != 0) && (temperature < 32) D. (temperature == 0) || (temperature > 32) E. (temperature < 0) || (temperature > 32)

(temperature == 0) || (temperature > 32), (temperature < 0) || (temperature > 32)

rules about IDs

-case-sensitive -cannot contain spaces -must begin with a letter, may be followed by any number of digits and letters. -can contain hyphens, underscores, colons, periods

Consider the following flow chart showing a process for program execution. Given that a = 5, and b = 10, what will be displayed by the program? if a<b console.log(b) else console.log("a is bigger")

10

A programmer designed a program for an airline to determine whether there is an extra fee on a checked bag. The logic is shown in the flow chart at right. The code they wrote (see below) runs without error, but unfortunately it does not work as intended. What is the problem? Consider the list of values below and choose two that if assigned to weight will result in the incorrect message being displayed as the response (choose 2)? 1. var weight = promptNum("How much does your luggage weigh?"); 2. if (weight > 50){ 3. setText("response", "There is a 25 dollar fee to take this luggage"); 4. } else if (weight > 120){ 5. setText("response", "Your luggage is too heavy for this flight"); 6. } else { 7. setText("response", "Your luggage is accepted as is"); 8. } A. 30 B. 50 C. 75 D. 125 E. 200

125, 200

What is the output of the following JavaScript code segment? var str1 = "5"; var str2 = "5"; var result = str1 + str2; console.log(result);

55

What is the output of the following JavaScript code segment? var num = 5; var str = "hello"; var result = num + str; console.log(result);

5hello

num1 - num2;

All programming languages support the basic arithmetic operations of addition, subtraction, multiplication and division of numbers. Operator precedence is the same as standard arithmetic - Expressions within a parentheses have the highest precedence, then multiplication and division, then addition and subtraction.

value1 + value2;

All programming languages support the basic arithmetic operations of addition, subtraction, multiplication and division of numbers. The addition operator is also used to add, or concatenate, two strings together. Operator precedence is the same as standard arithmetic - Expressions within a parentheses have the highest precedence, then multiplcation and division, then addition and subtraction. If either one of the operands are a string it treats the addition as string concatenation.

Which of the following is FALSE about element IDs? A. An element with a unique ID must always have an event handler associated with it. B. Any element that needs to be triggered by onEvent must have a unique ID. C. Two or more onEvent calls may reference the same ID. D. While not a requirement, IDs should be meaningful and descriptive. E. IDs allow a programmer to reference interface elements within their code.

An element with a unique ID must always have an event handler associated with it.

x = _____;

Assigns a value to a previously declared variable. The variable must be declared using var before it can be assigned its initial value. You can use the same variable on both the right hand side of the assignment operator = and the left hand side. This is sometimes used for a counter count = count + 1; = is the assignment operator. == is the boolean check for equivalency operator.

A programmer wrote an essay for his history class, and realized he has confused the names of Benjamin Franklin and Alexander Graham Bell. Instead of going through the whole paper and changing the names, he used the following incorrect algorithm in an attempt replace every occurrence of "Benjamin Franklin" with "Alexander Graham Bell" and vise versa: First, change all occurrences of "Benjamin Franklin" to "apple" Then, change all occurrences of "apple" to "Alexander Graham Bell". Then, change all occurrences of "Alexander Graham Bell" to "Benjamin Franklin". Here is an example of one of the sentences from the paper: Alexander Graham Bell was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors. Which of the following is the result of running the described incorrect algorithm on the sentence above? A. Benjamin Franklin was born 141 years before Alexander Graham Bell, so he was never able to telephone his neighbors. B. apple was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors. C. Alexander Graham Bell was born 141 years before apple, so he was never able to telephone his neighbors. D. Benjamin Franklin was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors. E. Alexander Graham Bell was born 141 years before Alexander Graham Bell, so he was never able to telephone his neighbors.

Benjamin Franklin was born 141 years before Benjamin Franklin, so he was never able to telephone his neighbors.

Consider the code segment below: var dist = 100; var radius = dist/20; penDown(); for(var i=0; i < 4; i++){ moveForward(dist); dot(radius); turnRight(90); } Which of the following images is the most likely outcome of the drawing? Answer Options: page 1 - https://docs.google.com/document/d/1H9F3_K2Vfjh0MGzNpNjENvMUK3Lz2n-yrymuqedKMKQ/edit?usp=sharing

C

What is a possible output when the following code segment executes? The ending position of the turtle is shown in each diagram. The starting position is shown as a white triangle in cases where the turtle starts and ends in different locations. var turn = 0 for (var i =0; i < 4; i++) { moveForward (); if(turn==0){ turnLeft (); turn = 1; }else{ turnRight(); turn = 0 } } A. ____ |___^ B. ____ ^___| C. ^___ |___ | ^ D. ___^ ___| | ^ E. ^ | | | | ^

C. ^___ |___ | ^

var = promptNum("Enter a value");

Declares a variable and prompts the user for its initial numeric value. The pop-up window only allows the user to enter a number. Use prompt() if the user is allowed to enter non-numeric data. The block of code where you declare the variable defines the variable's scope. Scope refers to which blocks of code can access that variable by name. For instance, if you declare a variable inside a function, that variable name can only be accessed inside that function. Variables declared at the top of your program are global and can be accessed anywhere in your program. Excessive use of promptNum() can get annoying, use this sparingly.

var x = prompt("Enter a value");

Declares a variable and prompts the user for its initial value.

var x = "_____";

Declares and assigns an initial string to a variable.

var x = _____;

Declares and assigns an initial value to a variable. You don't strictly need to provide a variable with an initial value when you create it, but it is a good practice, because if you accidentally use a variable that has never been assigned a value you can get unpredictable results in your code. Variables can store numbers, strings, arrays or objects. The block of code where you declare the variable defines the variable's scope. Scope refers to which blocks of code can access that variable by name. For instance, if you declare a variable inside a function, that variable name can only be accessed inside that function. Variables declared at the top of your program are global and can be accessed anywhere in your program. = is the assignment operator. == is the boolean check for equivalency operator.

Which of the following are actions a programmer could take when debugging a segment of code that would most likely lead to finding a problem and fixing it? (choose two) A. Change the names of variables within the program and run the program again. B. Display the value of variables at various points during the program. C. Ask a friend or collaborator to look over the code segment to see if they are able to find any errors. D. Delete the code and re-type it to make sure there were no spelling errors and that it was written correctly.

Display the value of variables at various points during the program. Ask a friend or collaborator to look over the code segment to see if they are able to find any errors.

console.log

Displays a string and/or variable values in the debug console in App Lab. console.log() is used as a debugging tool to help you understand what your code is doing. you can figure out -the order in which things are happening -value/state of something at various points in the program during execution -whether or not an event is firing and when The user of your app will not see the console.log() messages.

write(text)

Displays a string and/or variable values to the app screen. The text can also be formatted as HTML.

num1 / num2

Divides two numbers.

What is the output to the console after the following code segment is executed? var x = 10 increase(); x = x+3; console.log(x); function increase (){ var x = 5; }

Error. Cannot make a new variable x inside function increase()

What is the output to the console after the following code segment is executed? fiveMore() function fiveMore(){ var x = 5; } var y = 3 + x; console.log(y);

Error. Unknown Identifier: x

if(){//code}

Executes a block of statements if the specified condition is true. Unlike an event handler, an if statement does not constantly monitor your program checking the condition to see if it's true or false. An if statement is an instruction just like any other that gets executed line by line in order from top to bottom.

if(){//if code} else {//else code}

Executes a block of statements if the specified condition is true; otherwise, the block of statements in the else clause are executed. Unlike an event handler, an if/else statement does not constantly monitor your program checking the condition to see if it's true or false. An if statement is an instruction just like any other that gets executed line by line in order from top to bottom.

getText(id)

Gets the text from the specified screen element. getText() can also read the text on a button() or textLabel().

str.length In JavaScript you can find the number of characters in String by using .length. For example: var name = "Victoria"; console.log(name.length); // will display 8 - the number of characters in "Victoria" For the next two questions, consider the following JavaScript code segment. var word = prompt("Enter a word"); if( word.length > 10){ console.log("Wow, big word."); } else{ if( word.length < 3 ){ console.log("Did you say something? That was so short."); } else{ if( word.length > 6){ console.log("Use that word on the SAT."); } else{ console.log("I have never heard that before."); } } } If the above code segment is run, and "hello" is entered at the prompt, what will be displayed in the console?

I have never heard that before

_____&&_____

More complex decisions sometimes require two things to be true.

num1 * num2

Multiplies two numbers.

setPosition(id, x, y, width, height)

Positions an element at an x,y screen coordinate, and optionally sets a new width and height for the element.

str.toLowerCase

Returns a new string that is the original string converted to all lower case letters.

str.toUpperCase

Returns a new string that is the original string converted to all upper case letters.

randomNumber(min,max)

Returns a random number in the closed range from min to max. Negative values for parameters min or max are allowed. If you accidently make min larger than max it will still return a random number in the range. The number returned is not truly random as defined in mathematics but is pseudorandom.

!_____

Returns false if the expression is true; otherwise, returns true. You can stick a NOT (!) in front of any boolean expression to invert its result. This opens the door to express the same logical statements in different ways.

_____||_____

Returns true when either expression is true and false otherwise.

setScreen(screenId)

Sets the screen to the given screenId.

setText(id, text)

Sets the text for the specified screen element. to clear the text on a screen element set the text to be "". Make sure you getText() first if you need to save the data from a textInput() to a variable.

setSize(id, width, height)

Sets the width and height for the UI element.

Which of the following statements about strings in JavaScript is FALSE? A. Strings consist of a sequence of concatenated characters. B. Strings are indicated by quotation marks. C. Strings with numerical digits in them are invalid. D. A string can be empty, meaning that it contains nothing. E. Strings sometimes include spaces.

Strings with numerical digits in them are invalid.

_____>_____

Tests whether a value is greater than another value. When comparing two strings, JavaScript will compare them alphabetically based on character by character comparison left to right. All the upper case letters come before the lower case letters.

_____>=_____

Tests whether a value is greater than or equal to another value.

_____<_____

Tests whether a value is less than another value.

_____<=_____

Tests whether a value is less than or equal to another value.

_____ == _____

Tests whether two values are equal. JavaScript will automatically perform type conversion for you when comparing two values (e.g. the integer 5 will register as equivalent to the string "5").

____!=_____

Tests whether two values are not equal.

Consider the code segment below given in pseudo code (see reference above). Assuming that variables a, b and c already have numeric values assigned to them, what is the expected output of this code segment? val <- a IF(b >= a AND b >= c){ val <- b; } ELSE { IF (c >= a AND c >= b) { val <- c; } } DISPLAY (val);

The code will display the largest of the three values

Which of the following is FALSE about event-driven programs? A. Event-driven programs do not implement algorithms. B. Some portions of an event-driven program may never execute while the program is running. C. An event-driven program is written to respond to specified events by executing a block of code or function associated with the event. D. The order in which an event-driven program will run cannot always be known ahead of time. E. Event-driven programs can be run multiple times with different outcomes, based on user interactions.

The order in which an event-driven program will run cannot always be known ahead of time.

Consider the following JavaScript code segment. var time = promptNum("What hour is it (on a 24 hour clock)?"); var greeting = ""; if (time < 6) { greeting = "It is too early!"; } else if (time < 20) { greeting = "Good Day!"; } else if (time < 10) { greeting = "Good Morning!"; } else { greeting = "Good Evening!"; } console.log(greeting); Something is wrong with the logic in the program above. For which values of time will the greeting "Good Morning" be displayed?

There is no value of time that will result in "Good Morning" being displayed

A Boolean expression is an expression that evaluates to which of the of the following?

True/False

onEvent(id, type, function(event)) {...}

UI Control, for diff types of user interaction. UI elements, w unique ids, must exist before the onEvent function can be used.

str.length In JavaScript you can find the number of characters in String by using .length. For example: var name = "Victoria"; console.log(name.length); // will display 8 - the number of characters in "Victoria" For the next two questions, consider the following JavaScript code segment. var word = prompt("Enter a word"); if( word.length > 10){ console.log("Wow, big word."); } else{ if( word.length < 3 ){ console.log("Did you say something? That was so short."); } else{ if( word.length > 6){ console.log("Use that word on the SAT."); } else{ console.log("I have never heard that before."); } } } If the above code segment is run, and "goodbye" is entered into the prompt, what will be displayed in the console?

Use that word on the SAT.

rgb(r, g, b, a)

Using RGBA values, creates colors for using in the background, shapes, lines and points. Explore the 256^3 possible colors you can choose from using three numeric parameters. Note that if only one parameter is provided to rgb(), it will be interpreted as a grayscale value. Add a second parameter, and it will be used for alpha transparency for grayscale. When three parameters are specified, they are interpreted as RGB values. Adding a fourth parameter applies alpha transparency to the RGB color. if any of the RGBA values is out of range (0-255), then the value is rounded into range. For example, 300 becomes 255 and -100 becomes 0.

What is the expected output of the given pseudo code segment? A reference for the pseudo code can be found immediately below the question age <- 35 IF (age < 35) { DISPLAY ("You are not old enough to be President.") } ELSE { DISPLAY ("You are old enough to be President!") }

You are old enough to be President!

setProperty(id, property, value)

You will generally want to define properties of UI elements using Design mode in App Lab. But sometimes you will want to change the value of a property in your app based on the user or in response to an event. setProperty() lets your app change any property listed in Design mode for a given UI element. If you select a UI element that was created in Design mode, the dropdown for property will filter to just the possible set of properties that can be set for the UI element. getProperty() can be used to get the current value of a given property.

new line

\n

Consider the code segment below: var a = 0; var b = 3; var c = 4; a = a+c; b = a+c; c = a+c; What are the values of a, b, and c after this code segment has been run?

a = 4, b = 8, c = 12

event listener

a command that can be set up to trigger a function when a particular type of event occurs on a particular UI element. e.g. onEvent block

callback function

a function specified as part of an event listener; it is written by the programmer but called by the system as the result of an event trigger. called by the system at the time the specified event occurs. a common pattern in a lot of event-driven programming

selection

a generic term for a type of programming statement (usually an if-statement) that uses a Boolean condition to determine, or select, whether or not to run a certain block of statements a computer chooses to run one of two or more sections of code. used when making an if-statement

variable

a placeholder for a piece of information that can change a container for storing a value. once created, is stored in the computer's memory and can be used and updated repeatedly throughout your program.

algorithm

a precise sequence of instructions for a process that completes a task. can be executed by a computer and implemented using programming languages.

event-driven program

a program designed to run blocks of code or functions in response to specified events (e.g. a mouse click) users trigger events, events trigger code

Boolean

a single value of either TRUE or FALSE named after mathematician George Boole, discovered stuff about binary true/false values

global variable

a variable whose scope is "global" to the program, it can be used and updated by any part of the code. its global scope is typically derived from the variable being declare (created) outside of any function, object, or method

local variable

a variable with local scope is one that can only be seen, used, and updated by code within the same scope. typically this means the variable was declared (created) inside a function -- includes function parameter variables.

Can you figure out the values of a, b, and c? Using what you know about variables and re-assignment you should be able to trace this code and keep track of the values of a, b, and c as they change. You should also have a sense of any possible errors that could occur. Trace the code, and choose the option that correctly shows what will be displayed. var a = 3; var b = 7; var c = 10; a = a + b; b = a + b; c = a + b; console.log("a:"+a+" b:"+b+" c:"+c);

a:10 b:17 c:27

UI elements all function basically the same way

all UI elements have IDs and can listen for user events. Labels can have a background color but are designed to be filled with more text. Buttons have a default styling that changes slightly in color when clicked. buttons can also have a background image and a background color at the same time. Images can act a lot like buttons, only they cant' have text or a background color. Screens have IDs and you can use onEvent w them but if you use onEvent w a screen, it will capture every event of the type you specify regardless of whatever other elements are clicked on. there are no rules for which UI elements to use, just do what u want

data type

all values in a programming language have a "type" - such as a Number, Boolean, or String - that dictates how the computer will interpret it. for example 7+5 is interpreted differently from "7"+"5"

when is a click on the screen triggered

always, events that occur on the screen cannot be blocked by other objects. if u set up an event listener, it will capture EVERY event of that type, no matter what other UI elements are on the screen.

event

an action that causes something to happen controls, like click, scroll, move mouse, type keyboard key, etc.

event handling

an overarching term for the coding tasks involved in making a program respond to events by triggering functions the coding tasks involved in making your app respond to events by triggering functions, starts with adding onEvent.

string

any sequence of characters between quotation marks (ex. "hello", "42", "this is a string!")

expression

any valid unit of code that resolves to a value

App Lab apps

are actually web pages visual elements made w HTML (hypertext markup language, responsible for main visual components) and CSS (cascading style sheets, control styling of elements e.g. color, position, size) controlled by JavaScript (commands, buttons/design, inserting HTML and CSS)

how to add more complexity to conditional statements?

ask two questions at once using AND and OR operators. If first OR second expression true, OR true. if first and second expression false, OR false. if first AND second express true, AND true if first or second expression false, AND false.

console.log("with quotes")

asking computer to write those literal ASCII characters on the screen

=

assignment operator, read as "gets the value"

Jasmine is writing a shopping app. She has created a variable to keep track of the number of items in the shopping cart. Every time someone clicks the "addItemButton", she would like the variable to increase by 1. var cartTotal = 0; onEvent("addItemButton", "click", function() { // <missing code> console.log(cartTotal); }); What code should Jasmine insert where it says in order for her app to work? cartTotal = cartTotal +1;

cartTotal =cartTotal + 1;

if-else statement

commands contained in the else statement only run if the Boolean condition in the if statement is false.

console.log(without quotes)

computer assumes you're referring to a variable called without quotes and will attempt to retrieve its value. if variable hasn't been made, computer will give u an error.

Which of the following JavaScript statements will result in the following output being displayed to the console? "Hello! How are you?" A. console.log("Hello! \tHow are you?"); B. console.log("Hello! \bHow are you?"); C. console.log("Hello! \newLineHow are you?"); D. console.log("Hello! \nHow are you?"); E. console.log("Hello! \nlHow are you?");

console.log("Hello! \nHow are you?");

variable scope

dictates what portions of the code can "see" or use a variable, typically derived from where the variable was first created

debugging

finding and fixing problems in an algorithm or program.

how are tasks automated?

identify step-by-step processes that we know work well, can be then used by machines. automation requires algorithms, many information tasks can be completed using.

A pseudocode program is started below that asks the user for input and stores the value in a variable. Continue writing pseudocode to accomplish this task: If the hour is within the school day (8 to 15) then display "Nice to see you!", Otherwise, display "It's time to go home! DISPLAY ("Enter the hour of day (0-23)") hour <- INPUT ()

if (hour >=8&&hour<=15) { DISPLAY ("Nice to see you!") }else{ DISPLAY ("It's time to go home!")

+

if both operands are numbers, the two numbers are added together, standard additions. works with numbers and variables that contain numbers. if either of the operands is a string, + treats both as if they were strings and combines them to create a single string

Boolean Expression

in programming, an expression that evaluates to True or False.

NaN

not a number

UI elements

on-screen objects, like buttons, image, text boxes, pull down menus, screens, and so on.

Two students, Kaleb and Hunter, are arguing in class about an App Lab project. Kaleb states, "Huh, a button and an image are basically the same thing!". Hunter replies, "That doesn't make any sense at all!". Explain what Kaleb may have meant by that statement.

one quizlet : Kaleb may have meant since UI elements or objects are buttons, images, text boxes, pull-down menus, screens, and etc that they are the same. Not necessarily what they are, just what they fall into when talking about the user interface. my answer : Kaleb may have meant that without an event-driven program attached to a button, it is nothing more than a static image. Additionally, buttons may take the form of images if they don't have any code attached to them if the user simply changes settings for the button's appearance

if-else-if statements

order to commands matters. most specific condition must be entered first

sequence

placing commands in an order. defining the order in which a computer should run the fundamental commands it understands.

When might a programmer create a global variable instead of a local variable?

quizlet answer: A global variable would be created if the variable is used throughout the program and not just in one specific function; ex. score needs to be changed when divided(function) my answer: A programmer might create a global variable instead of a local variable when they want to make a variable that can be used and modified for the duration of the program. Local variables can only be used and modified within the function they are in, and once the function is done, the variable is destroyed.

what would result = 5 + 7 save as to a computer?

result = 12, not result = 5 + 7

conditionals

statements that only run under certain conditions

string rules

strings -can contain spaces "wow cool" -can contain special characters "! ? ..;[;.[$" -are able to contain only digits "11" -can contain no characters "" -can be stored in a variable -are concatenated with +

common types of errors

syntax errors -misspellings, miscapitalizations, etc. -computer usually gives error message to clue u in -read over erred lines carefully logic errors -program runs, but not how u want it to -no error messages -retrace ur steps and try to think about ur code like a computer

if-statement

the common programming structure that implements "conditional statements"

==

the equality operator (sometimes read: "equal equal") is used to compare two values, and returns a Boolean (true/false). avoid confusion with the assignment operator "="

what happens when you click on overlapping objects?

the event-handler on top gets triggered

context sensitive

the set of properties you see depends on what you click on in the app.

user interface

the visual elements of a program through which a user controls or communicates with the application. often abbreviated UI. how a person (user) interacts with the computer or app.

concatenate

to link together or join. typically used when joining together text Strings in programming (e.g. "Hello, "+name)

why use global variables?

useful for keeping track of data over the lifetime of the program that's running. use if -u want to keep track of some data btwn events/btwn function calls created and initialized in the very first lines of a program's code. global variable can be created anywhere in code outside of function definition so assign it some random value at the top of the program for organization

why use local variables?

useful temporary placeholders if u wanna do a computation ex: function parameters. created/initialized when a function is called, get used while function runs, are destroyed when it's done. if all variables had to be global, we'd have to invent diff variable names for every function parameter in the whole program, but if something is local to a function, can reuse parameter name anytime. if your local variable has the same name as a global variable, the function will try to use the local one first.

What is displayed by the console.log statement after the following code segment executes? var a = 3; var b = 6; var c = 10; a = b / a; b = c - a; c = b / a; console.log("value is: "+c);

value is: 4

A student decides to draw a series of three dots (sort of like a snowman) as shown in the diagram. She wants each dot to be half the radius of the previous dot, and for the center to be on the edge of the dot below it. She writes the following code segment to do it: var bottom = 100; var middle = 50; var top = 25; dot(bottom); moveForward(bottom); dot(middle); moveForward(middle); dot(top); She is not sure about the size though, and wants to be able to quickly experiment with the drawing by changing only one number - the radius of the bottom dot - and for the rest of the code to size and scale the drawing accordingly. How should she adjust lines 2 and 3 of her code to implement this change?

var middle = bottom / 2 ; var top = middle / 2 ;

Jose is writing a reply function for a text messaging app. He'd like to swap the sender and receiver so that the value currently in variable From ends up as the value in To and To ends up in From From [James Bond] From [Bruce Wayne] ^ ^ \ / X To [Bruce Wayne] / \ To[James Bond] v v Which of the following code segments will correctly swap the values as described?

var temp = from; from = to; to = temp;

iteration

when a computer repeats a section of code. can be done by using a loop.


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

Chapter 24 Preterm Complications

View Set

Mod 9: Chapter 30 Abdominal and Genitourinary Injuries, Chapter 31 Orthopaedic Injuries, Chapter 32 Environmental Emergencies

View Set

Standard of Living and Quality of Life

View Set

Interpersonal Communications Final

View Set

Nclex pass point questions set 1

View Set