









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Introduction to Programming with Python & Java: Part 1 || 100% Actual Solutions.
Typology: Exams
1 / 16
This page cannot be seen from the preview
Don't miss anything!










Which of the following is NOT a kind of client-side programming (computing) language? A. HTML B. Python C. CSS D. JavaScript correct answers B. Python Which of the following is NOT a kind of server-side programming language? A. HTML B. Python C. Java D. PHP correct answers A. HTML Which of the following statements is NOT correct? A. Python is an open-source programming language B. Python needs to be compiled C. Python is an object-oriented programming (OOP) language D. Python is interpreted by a Python interpreter correct answers B. Python needs to be compiled Which of the following will return an error? A. print(round(15387-128) + int(412//23)) B. print('23 % 11 = ' + 23 % 11) C. print('Today's a good day') D. print(4>4) correct answers B. print('23 % 11 = ' + 23 % 11) Which of the following can be used for exponentiation? A. // B. ** C. % D. * correct answers B. ** Which of the following statements are true? Select all that apply. A. A variable is a symbolic name for (or reference to) a value. B. A variable can be used to store all kinds of stuff including user input. C. Variable names are case-sensitive D. You can assign a variable a value and then reassign the same variable a different value later on in your code. correct answers All of the above Which of the following is a comparison operator? A. / B. and C. **
D. > correct answers D. > What data type could be used to store the phone number 123-456-7890? A. int B. float C. bool D. str correct answers D. str Which of the following is the correct syntax to check if the variable x is equal to 5? A. if (x != 5) B. if (x = 5) C. if (x <= 5) D. if (x == 5) correct answers D. if (x == 5) What does the following code output: x = 5, y = "John", z = "UIO", print(x + x + z)? A. 10John B. 5 UIO C. TypeError D. 10UIO correct answers C. TypeError What is the limit to the number of elif statements that can be implemented after an if statement? A. 5 B. 10 C. 100 D. There is no limit correct answers D. There is no limit What is used to define a block of code (body of loop, function, etc.) in Python? A. Indentation B. Parenthesis C. Quotation D. Curly braces correct answers A. Indentation What is used to take input from the user in Python? A. get() B. input() C. scanf() D. <> correct answers B. input() When defining a variable, which of the following is allowed? A. The variable name can start with a digit. B. The variable name can start with an underscore. C. The variable name can contain a space. D. The variable name can have symbols like @, #, $, etc. correct answers B. The variable name can start with an underscore.
C. An error message D. None of the above correct answers C. An error message What will be the result of the following script: my_list = [1, 2, 3, 4], my_list.append(5), my_list.pop(), my_list.pop(1), print(my_list)? A. [2, 4, 5] B. [1, 3, 4] C. [3, 4, 5] D. [2, 3, 4] correct answers B. [1, 3, 4] What will be the result of the following script: my_list = ['cat', 'dog', 'fish'], print('cat' in my_list)? A. cat B. True C. 1 D. 'cat' in my_list correct answers B. True What does the following script print: for i in range(5): print(i)? A. Numbers from 0 - 4 B. Numbers from 1 - 5 C. Numbers from 0 - 5 D. Number 5 only correct answers A. Numbers from 0 - 4 What will the following script print: x = 0, while x <= 5: if x == 4: break, print(x), x += 1 A. Numbers 0 - 4 B. Numbers 0 - 3 C. Numbers 1 - 4 D. Numbers 1 - 3 correct answers B. Numbers 0 - 3 What will the following script print: x = 0, for x in range(5): if x == 3: continue, print(x) A. 0, 1, 2, 3, 4 B. 1, 2, 3, 4 C. 0, 1, 2, 4 D. 1, 2, 4, 5 correct answers C. 0, 1, 2, 4 What will be the result of the following script: string = 'pasta', result = '', for i in range(len(string)
D. [0, 0, 1, 2] correct answers D. [0, 0, 1, 2] What does the following code output: l = ['foo', 'bar', 'baz'], while len(l) > 3: print(d.pop()), print('Done.')? A. The snippet does not generate any output. B. 'baz', 'bar', 'foo', Done. C. foo, bar, baz D. Done correct answers D. Done What does the following code output: for num in range(1, -5, -1): print(num, end=", ")? A. 1, 0, -1, -2, -3, -4, - B. -5, -4, 3, 2, 1, 0 C. -4, -3, -2, -1, 0, 2, 3, 4, 5 D. 1, 0, -1, -2, -3, -4, correct answers D. 1, 0, -1, -2, -3, -4, What does the following code output: num = 10, i = 9, if num % i == 1: print(num), break A. 10 B. 9 C. SyntaxError D. 1 correct answers C. SyntaxError What is the output of the following function call: def fun1(num): return num + 25, fun1(5), print(num)? A. 25 B. 5 C. NameError D. 30 correct answers C. NameError Choose the correct function definition of calc() so that we can execute the following function call successfully: calc(25, 75, 55), calc(10.2, 20.3, 4), calc('output', 10.2, 20.3). A. def calc(a, b, c): print(a * b * c) B. def calc(a, b, c): print(a, b / c) C. No, it is not possible in Python D. def calc(a, b, c): print(a + b + c) correct answers B. def calc(a, b, c): print(a, b / c) Which of the following statements about Docstrings is correct? A. A docstring describes the operation of the function (or class). B. To create a docstring, include a string as the first statement in function. C. The docstring is accessible by calling the function: help(func_name). D. All of the above correct answers D. All of the above What will be the result of the following script: def square(x): print(x ** 2), square(3)? A. 2
B. Tuples C. Sets D. Loops correct answers A, B and C Which of the following is a comment in Python? Select all that apply. A. //This is a single line comment B. #This is a single line comment C. """ This is a multi-line comment """ D. /* This is a multi line comment */ correct answers B and C What is the correct way to select the third element in a list called 'my_list'? A. my_list[2] B. my_list[3] C. my_list.get(2) correct answers A. my_list[2] What will be the result of the following code: lst = [1, 2, 3], lst.append(3), lst.pop() , print(lst)? A. [1, 2, 3] B. [1, 2, 3, 3] C. [2, 3, 3] D. Error correct answers A. [1, 2, 3] What will be the result of the following code: lst = [1, 2, 3], lst.append(3), lst.pop(0), print(lst)? A. [1, 2, 3] B. [1, 2, 3, 3] C. [2, 3, 3] correct answers C. [2, 3, 3] What will be the result of the following code: for i in range(5): print(i)? A. Print numbers from 0 - 5 B. Print numbers from 0 - 4 C. Print numbers from 1 - 5 D. Print numbers from 2 - 5 correct answers B. Print numbers from 0 - 4 What will be the result of the following code: myList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'], print(myList[6:8]) A. ['g', 'h'] B. ['g', 'h', 'a'] C. ['g'] D. Error correct answers A. ['g', 'h'] Which of the following is used to represent a list in Python? A. [] B. {} C. () D. All of above correct answers A. []
What will be the result of the following code: x = (1, 2, 3, 4), print(x[4])? A. Error B. 4 C. x D. None correct answers A. Error What will be the result of the following code: s = 'all', print(set(s))? A. {'a', 'l'} B. {'a', 'l', 'l'} C. {'l', 'a'} D. Both {'a', 'l'} and {'l', 'a'} are possible correct answers D. Both {'a', 'l'} and {'l', 'a'} are possible What will be the result of the following code: a = [1, 2, 3, None, (), []], print(len(a))? A. 6 B. 3 C. 4 D. None correct answers A. 6 What will be the result of the following code: my_list = ['cat', 'dog', 'fish'], print('cat' in my_list) A. 'cat' B. True C. 1 D. None correct answers B. True What will list(d.keys()) return; d = {'A': 1, 'B': 2, 'C': 3}? A. [1,2,3] B. 'A', 'B', 'C' C. [A, B, C] D. ['A', 'B', 'C'] correct answers D. ['A', 'B', 'C'] How would one delete the entry with key 'A'; d = {'A': 1, 'B': 2, 'C': 3}? A. d.delete('A': 1) B. d.delete('A') C. del d['A'] D. del d['A':1] correct answers C. del d['A'] What is the result of the following code: d = {}, d[1] = 1, d['1'] = 2, d[1] = 3, sum = 0, for i in d: sum += d[i], print(sum)? A. 2 B. 3 C. 5 D. 6 correct answers C. 5
D. f = my_file.open('r') correct answers A and B If you wanted to open a file "metrics.csv" in write mode using the following method, f = my_file.open('metrics.csv','w'), how would you then close the file? A. file.end() B. exit() C. f.close() D. metrics.close() correct answers C. f.close() What does the following code do: f = open("test.txt")? A. Opens test.txt file for both reading and writing B. Opens test.txt file for reading only C. Opens test.txt file for writing only D. Opens test.txt file in god mode correct answers B. Opens test.txt file for reading only What does the following code do: f = open("test.txt", "r+")? A. Opens test.txt file for both reading and writing B. Opens test.txt file for reading only C. Opens test.txt file for writing only D. Opens test.txt file in god mode correct answers A. Opens test.txt file for both reading and writing What will happen if you run the following code, f = open("test.txt", "a"), but "test.txt" does not exist? A. Creates "test.txt" and opens in read mode B. Creates "test.txt" and opens in append mode C. Does not create "test.txt" and no error message D. Error message correct answers B. Creates "test.txt" and opens in append mode Which of the following commands can be used to read all of the content in a file object f as a string? A. f.read() B. f.readline() C. f.readlines() D. f.read(n) correct answers A. f.read() What is the result of the following code: lst = [], with open("text.txt", "r") as f: lines = f.readlines(), for i in range(2): lst.append(lines[i].rstrip()), print(lst)? A. ['The world is hard and cruel.', 'We are here none knows why,'] B. ['The world is hard and cruel.\n', 'We are here none knows why,\n'] C. 'The world is hard and cruel. We are here none knows why,' D. 'The world is hard and cruel.\n We are here none knows why,' correct answers A. ['The world is hard and cruel.', 'We are here none knows why,']
What is the result of the following code: with open("text.txt", "w") as f_write: f_write.write("That is the wisdom of life.") , with open("text.txt", "r") as f_read: line = f_read.readline(), print(line)? A. 'The world is hard and cruel.' B. ['That is the wisdom of life.'] C. 'That is the wisdom of life.' D. ['We must be very humble.'] correct answers C. 'That is the wisdom of life.' Which of the following can be used to open a file "MOOC.csv" in read-mode only? A. my_file = open('MOOC.csv', 'r') B. my_file = open('MOOC.csv', 'r+') C. my_file = open(my_file = 'MOOC.csv', 'r') D. my_file = open(my_file = 'MOOC.csv', 'r+') correct answers A. my_file = open('MOOC.csv', 'r') Which of the following statements is false? A. When you open a file for writing, if the file does not exist, a new file is created. B. When you open a file for writing, if the file exists, the existing file is overwritten with the new file. C. When you open a file for reading, if the file does not exist, an error occurs. D. When you open a file for writing, if the file does not exist, an error occurs. correct answers D. When you open a file for writing, if the file does not exist, an error occurs. Which of the following can be used to open a file "MOOC.csv" in append-mode only? A. my_file = open('MOOC.csv', 'w+') B. my_file = open('MOOC.csv', 'a') C. my_file = open('MOOC.csv', 'rw') D. my_file = open('MOOC.csv', 'r+') correct answers B. my_file = open('MOOC.csv', 'a') Which of the statements below is false for the following script: import csv, with open('MOOC.csv', 'r') as csvfile: reader = csv.DictReader(csvfile)? A. It can run without any error B. The type of 'reader' is DictReader C. You can iterate over 'reader' more than once D. 'MOOC.csv' is opened for reading only correct answers C. You can iterate over 'reader' more than once Which of the statements below is true for the following script: import csv, youngest_student = None, min_age = None, with open('MOOC.csv', 'r') as csvfile: reader = csv.DictReader(csvfile), for row in reader: age = int(row["age"]), if min_age == None or min_age > age: youngest_student = row["name"], min_age = age? A. The script cannot be run as there is an error with the usage of DictReader. B. The script cannot be run as there is an error with iterating over the reader. C. The script can be run; the youngest student and their age can be found.
B. The script will print 'Error!' C. The script will print 'zero' D. The script will print '0.0' correct answers A. The script will cause a ValueError because the code that tries to catch the error is incorrect. We have the following information about students: student_info = [{'first name':'Caroline', 'last name':'Harrison', 'birthdate':'1997-03-02'}, {'first name':'Rachel', 'last name':'Reynolds', 'birthdate':'1996-01-07'}, {'first name':'Olivia', 'last name':'James', 'birthdate':'1998-05-09'}] Which of the following code snippets is correct to sort the given student_info based on last name? A. student_info.sort() B. student_info.sort(student_info['last name']) C. student_info.sort(key=lambda x: x['last name']) D. None of the above correct answers C. student_info.sort(key=lambda x: x['last name']) We have the following information about students: student_info = [{'first name':'Caroline', 'last name':'Harrison', 'birthdate':'1997-03-02'}, {'first name':'Rachel', 'last name':'Reynolds', 'birthdate':'1996-01-07'}, {'first name':'Olivia', 'last name':'James', 'birthdate':'1998-05-09'}] Which of the following code snippets is correct to get the information for the youngest student? A. youngest_student_info = max(key=lambda student_info: student_info['birthdate']) B. youngest_student_info = max(student_info.sort(student_info['birthdate'])) C. youngest_student_info = max(student_info['birthdate']) D. youngest_student_info = max(student_info, key=lambda x: x['birthdate']) correct answers D. youngest_student_info = max(student_info, key=lambda x: x['birthdate']) What is the result of the following code: import pandas as pd, xls = pd.ExcelFile('yelp.xlsx'), df = xls.parse('yelp_data'), print(type(df))? A. .xlsx B. DataFrame C. Data D. .docs correct answers B. DataFrame How can you quickly examine the first 5 rows of a DataFrame? A. df.head() B. df.head(4) C. df.describe(5) D. df.drop_duplicates() correct answers A. df.head() How can you get the type of data stored in each column of a DataFrame df? A. type(df) B. df.columns C. df.dtypes D. df.count() correct answers C. df.dtypes How many rows will there be after calling df.drop_duplicates("AUTHOR", inplace = True)?
D. 5 correct answers B. 1 Which is the correct way to select some columns of data in DataFrame df? A. df["id", "name", "gender"] B. df[["id", "name", "gender"]] C. df("id", "name", "gender") D. df (["id", "name", "gender"]) correct answers B. df[["id", "name", "gender"]] Which method do we use for joining two tables? A. pd.join() B. pd.merge(left=df, right=df_cities, how='inner', left_on='city_id', right_on='id') C. df.describe() D. df.head() correct answers B. pd.merge(left=df, right=df_cities, how='inner', left_on='city_id', right_on='id') What does the following code do: df[len(df) - 1 : ]? A. Returns rows from 0 to the last one B. Returns the first row C. Returns the last two rows D. Returns the last row correct answers D. Returns the last row What is the result of the following code: s = df["some_attribute"] == "some_value", type(s)? A. Series B. DataFrame C. Boolean D. List correct answers A. Series What does the following code do if "df" is a DataFrame that stores the movie information: genre = df["genre"].isin(["Comedy", "Action"]), year = df["release_year"] == 2000, rating = df["rating"] > 4, df[genre & (year | rating)]? A. Select the comedy movies and action movies that are released in 2000 or have ratings above
B. Select the comedy movies and action movies that are released in 2000 and have ratings above
C. Select comedy movies and action movies that are not released in 2000 or have ratings equal to
Assume we want to add a legend in a histogram. Which parameter should we specify if we want to place the legend at the center of the figure? A. loc B. center C. xy correct answers A. loc What does the following script do: plt.hist([series_pitt, series_vegas], alpha=0.7, color=['red','blue'], label=['Pittsburgh','Las Vegas'], bins="auto")? A. It creates two figures each containing a histogram plot. B. It creates two overlapping histograms in one figure. C. It plots two histograms whose bars are side by side in one figure. correct answers C. It plots two histograms whose bars are side by side in one figure. What do the first two arguments specify in the method scatter()? A. The labels for the x and y-axis. B. The data for the x and y-axis. C. The marker shape and color. correct answers B. The data for the x and y-axis. Is the following code the right way to set the scale of a y-axis as logarithmic: import matplotlib.pyplot as plt, plt.set_yscale('log')? A. Yes B. No correct answers B. No