7.7.1 Functions and Parameters Quiz

Ace your homework & exams now with Quizwiz!

What will the following program print to the screen when run? function start() { println(mystery(3, "*~*")); } function mystery(num, pattern) { var result = ""; for (var i = 0; i < num; i++) { result = result + pattern; } return result; } *~ *~* *~*~*~ *~**~**~*

*~**~**~*

What is printed by the following program? function printNumbers(two, one, zero){ println(two); println(one); println(zero); } function start(){ var zero = 0; var one = 1; var two = 2; printNumbers(zero, one, two); } 2 1 0 0 1 2 zero one two two one zero

0 1 2

In the following function printThreeTimes: function printThreeTimes(word){ println(word); println(word); println(word); } What is the parameter of the function? printThreeTimes function println word

word

What is the output of the following program? function start(){ var x = 5; sumTo(x); println(x); } function sumTo(num){ var sum = 0; for(var i = 0; i <= num; i++){ sum += i; } println(sum); } 5 15 15 5 5 15 none, there is a syntax error

15 5

How many parameters go into the function sum, and how many return values come out of the function sum? function sum(first, second, third){ var result = first + second + third; println(first); println(second); println(third); return result; } 3 parameters go in, 1 return value comes out 3 parameters go in, 3 return values come out 1 parameter goes in, 4 return values come out 1 parameter goes in, 1 return value comes out

3 parameters go in, 1 return value comes out

What will be printed to the screen when the following program is run? function start(){ println(doubleNumber(doubleNumber(10))); } function doubleNumber(x){ var doubledX = 2 * x; return doubledX; } 10 20 40 100

40

What is printed by the following program? function product(x, y){ return x * y; } function difference(x, y){ return x - y; } function start(){ var x = 2; var y = 5; var value1 = product(x, y); var value2 = difference(y, x); var result = difference(value1, value2); println(result); } 7 -7 13 -13

7

Why do we write functions? Make our code easier to understand by giving a readable name to a group of instructions Avoid writing repeated code Make our code reusable All of the above

All of the above

Consider the program below: var RADIUS = 35; function start() { var centerX = getWidth() / 2; var centerY = getHeight() / 2; drawCircle(RADIUS, Color.red, centerX, centerY); drawCircle(RADIUS * 2, Color.red, centerX, centerY); drawCircle(RADIUS * 4, Color.red, centerX, centerY); } function drawCircle(radius, color, x, y) { var circle = new Circle(radius); circle.setColor(color); circle.setPosition(x, y); add(circle); } Which is NOT a reason to use a global variable, such as RADIUS in the above program? Any function in the program can use it You can alter many values used in the code by only changing one value It represents a constant value that isn't expected to change anywhere in the program It allows you to use its value anywhere in the program by typing RADIUS or radius

Bypass the need to give the variable as a parameter to be used in a function

Which of the following is NOT a use of the function drawCircle in the following program? var RADIUS = 35; function start() { var centerX = getWidth() / 2; var centerY = getHeight() / 2; drawCircle(RADIUS, Color.red, centerX, centerY); } function drawCircle(radius, color, x, y) { var circle = new Circle(radius); circle.setColor(color); circle.setPosition(x, y); add(circle); } Draw circles in various positions on the canvas Draw circles with various colors on the canvas Draw different shapes on the canvas Draw circles with various sizes on the canvas

Draw different shapes on the canvas

In the following code: function drawCircle(radius, x, y, color) { var circle = new Circle(radius); circle.setPosition(x, y); circle.setColor(color); add(circle); } What is the minimum number of parameters that must be given when calling the 'drawCircle' function? Four Three Two One

Four

In the following code: function start(){ var length = 5; var width = 4; rectangleArea(length, width); } // This function prints out the area of // a triangle given its base and height function rectangleArea(base, height){ var area = base * height; println(area); } What are the names of the parameters? length and width length and height base and height base and width

base and height

"It's a bird! It's a plane! No, it's Superman!"We want to write a function isSuperman that takes in two parameters isBird and isPlane and returns true if it is in fact Superman, and false otherwise.If it's not a bird and it's not a plane, it must be Superman.Which of the following functions is the correct implementation of isSuperman? function isSuperman(isBird, isPlane){ return isBird || isPlane; } function isSuperman(isBird, isPlane){ return !isBird || !isPlane; } function isSuperman(isBird, isPlane){ return !isBird && !isPlane; } function isSuperman(isBird, isPlane){ return isBird && isPlane; }

function isSuperman(isBird, isPlane){ return !isBird && !isPlane; }

Consider the following functions: function fnA(num) { if (num > 3){ println ("Fizz"); } } function fnB(num) { if (num > 2 && num < 4){ println ("Fizz"); } } Which start function definition would print: Fizz Fizz function start() { for (var i = 0; i < 6; i++) { fnB(i+1) } } function start() { for (var i = 0; i < 6; i++) { fnA(i) fnB(i) } } function start() { for (var i = 0; i < 6; i++) { fnA(i) } } function start() { for (var i = 0; i < 6; i++) { fnB(i) } }

function start() { for (var i = 0; i < 6; i++) { fnA(i) } }

In the following code: var MAX_NUMBER = 10; function start(){ var num1 = Randomizer.nextInt(0, MAX_NUMBER); var num2 = Randomizer.nextInt(0, MAX_NUMBER); println( sum(num1, num2) ); } function sum(x, y){ var result = x + y; //Point A return result; } Which variables exist at point A? In other words, which variables are in scope at point A? Which variables could you type at point A and the program would still work? result only result, x, and y result, x, y, and MAX_NUMBER result, num1, num2, and MAX_NUMBER result, x, y, num1, num2, and MAX_NUMBER

result, x, y, and MAX_NUMBER


Related study sets

Carbon Fixation: The C3 and C4 Pathways

View Set

Alexander Fleming/Discovering Penicillin

View Set

European Union - Governments & Politics of Western Europe

View Set

AWS Academy Cloud Foundations Knowledge Check

View Set