CSCI 344: Fall 2024

Advanced Web Technology

CSCI 344: Fall 2024

UNCA Logo

Pre-Course Assessment

1. Write a function called add_nums that takes two numbers as arguments and returns their sum.

If I invoke your function as follows, it will return the number 9: add_nums(4, 5)

JavaScript

function add_nums(num1, num2) { 
    return num1 + num2; 
}
const result = add_nums(4, 5);
console.log("The sum is:", result);

Java

public class Main {
    public static int add_nums(int num1, int num2) {
        return num1 + num2;
    }

    public static void main(String[] args) {
        int result = add_nums(4, 5);
        System.out.println("The sum is: " + result);  // Output will be 15
    }
}

Python

def add_nums(num1, num2):
    return num1 + num2

result = add_nums(4, 5)
print("The sum is:", result)  # Output will be 15

2. Write a loop (any kind of loop you want) that prints the integers from 1 to 100 in order (e.g., 1, 2, 3, …, 99, 100).

Don’t worry about line breaks.

JavaScript

// Using a for loop
for (let i = 1; i <= 100; i++) {
    console.log(i);
}

Java

for (int i = 1; i <= 100; i++) {
    System.out.println(i + " ");
}

Python

# Using a for loop
for i in range(1, 101):
    print(i)

3. What is the output of this code block:

Written in Python

a = 3
b = 2
while b > 0:
   a -= b
   b += a
   print(a, b)

Written in Java

int a = 3;
int b = 2;

while (b > 0) {
    a -= b;
    b += a;
    System.out.println(a + " " + b);
}

Answer:

 1  3
-2  1
-3 -2

video walkthrough

4. What will print to the screen, given the following code block:

Written in Python

a = True
b = True
c = False


if a and c:
   print('squirrel')
elif a:
   print('lion')


if a and not c:
   print('cat')
elif a:
   print('dog')
elif not c:
   print('penguin')


print('giraffe')

Written in Java

boolean a = true;
boolean b = true;
boolean c = false;

if (a && c) {
    System.out.println("squirrel");
} else if (a) {
    System.out.println("lion");
}

if (a && !c) {
    System.out.println("cat");
} else if (a) {
    System.out.println("dog");
} else if (!c) {
    System.out.println("penguin");
}

System.out.println("giraffe");

Answer:

lion
cat
giraffe

video walkthrough