Free Response Questions

What keyword do we use in Java to show inheritance?

Answer: extends
Tag(s): object oriented programming, Java, inheritance,
Subject: Computer Science

What is a method?

Answer: A method is a storage location for related program statements. When called, a method usually performs a specific task. (Something along these lines)
Tag(s): object oriented programming, classes, Java, Python,
Subject: Computer Science

Write a method called print5 that prints the number 5.

Answer: public void print5() { System.out.println(5); //.print(5) is also acceptable }
Tag(s): object oriented programming, Java, methods,
Subject: Computer Science

Explain the three different ways of reading in a file in Python (.read, .readline, .readlines)?

Answer: .read - reads in an entire document as one string. .readlines - reads in an entire document as individual lines. .readline - reads in a document line by line.
Tag(s): Python, file io,
Subject: Computer Science

The following class definition has errors in it. Identify and correct the mistakes.

public class PupperDoggo()
{
    public void sayName
    [
        println(“Doge!);
    ]
}

Answer: 1. No () after class definition 2. Forgot () after sayName 3. No square brackets for method. Use { } 4. Forgot out or System.out for println 5. No closing quote at Doge!
Tag(s): Java, debugging,
Subject: Computer Science

Define Encapsulation.

Answer: It is the process of associating variables with a class object. The variables are “enclosed” within the object.
Tag(s): object oriented programming,
Subject: Computer Science

Explain the output for the following block of code:

for letter in "lemon pies".split("e"):
          print(letter)

Answer: .split() will separate the text within the quotes on the letter "e" and print each element on its own line: l mon pi s
Tag(s): for loops, Python,
Subject: Computer Science

What is the name of the class used in Java we use to read from the console and can use to read from a file?

Answer: Scanner
Tag(s): Java, file io,
Subject: Computer Science

What is the snippet of code we need to read in the next integer from our IO stream and assign it to the int called kevin? Assume your stream object is named file.

Answer: kevin = file.nextInt();
Tag(s): Java, file io,
Subject: Computer Science

What is the snippet of code we need to read in the next character from our IO stream and assign it to a new variable rudolf (keyboard or file)? Assume your stream object is named kb.

Answer: char rudolf = kb.next().charAt(0);
Tag(s): Java, file io,
Subject: Computer Science

What type of loop should we use if our data file is formatted where the first integer is the number of remaining items in the file and the remaining items are data for processing? Explain.

Answer: For loop. We know how many items are in the file beforehand, therefore know how many times the loop needs to run.
Tag(s): for loops, Java, file io,
Subject: Computer Science

What type of loop should we use if our data file is formatted where all the data within the file is data that needs to be processed and we can’t know how much data is in the file? Explain.

Answer: While loop. Since we don’t know how much data there is we need to run until the end of the file.
Tag(s): while loops, Java, file io,
Subject: Computer Science

Write the code that reads in a file (nums.dat) of unspecified length that contains integers and prints the sum of all the integers in the file. (Include the import, main function, object instantiation, and any other necessary code)

Answer: import java.io.*; import static java.lang.System.*; import java.util.*; public static void main(String [] args) throws IOException { Scanner file = new Scanner(new File(‘nums.dat’)); int sum = 0; while(file.hasNextInt()) { sum += file.nextInt(); } out.println(sum); }
Tag(s): while loops, Java, file io,
Subject: Computer Science

Write the code that reads in a file (names.dat) where the first int n, contains the number of data sets to follow and the remaining data is the names of students in a class. Your code should print out the name of every student in the class in all caps. (Include the import, main function, object instantiation, and any other necessary code)

Answer: import java.io.*; import static java.lang.System.*; import java.util.*; public static void main(String [] args) throws IOException { Scanner file = new Scanner(new File(‘names.dat’)); int num_names = file.nextInt(); file.nextLine(); for(int x = 0; x < num_names; x++) { out.println(file.next().toUpperCase()); } }
Tag(s): for loops, Java, file io, strings,
Subject: Computer Science