Skip to content

Instantly share code, notes, and snippets.

@valen214
Last active January 5, 2020 11:41
Show Gist options
  • Save valen214/66eafdef48f4507f6509b86a86c01f8e to your computer and use it in GitHub Desktop.
Save valen214/66eafdef48f4507f6509b86a86c01f8e to your computer and use it in GitHub Desktop.
high school programming question
{
"java": {
"1": {
"title": "Basic I/O - Console",
"questions": [
{
"type": "mc",
"question": "Which of the following is the correct package import statement so that the following line <br />\n<pre>\nScanner scanner = new Scanner(System.in);\n</pre>\ncan compile without error?",
"options": [
"import java/util/Scanner",
"import java.util;",
"import java.util.Scanner;",
"import util/Scanner",
"import java.io.Scanner;",
"import java/io/Scanner",
"import java.io.scanner"
],
"answer": 2
},
{
"type": "fill",
"question": "given <pre>\nimport java.util.Scanner; // Import the Scanner class to read text files\n\npublic class ReadFile {\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n String line;\n do{\n line = scanner.nextLine();\n ___________________;\n } while(!line.isEmpty());\n scanner.close();\n }\n}</pre>\nwhat should be filled in '______________' to print whole user input line-by-line?",
"answer": "System.out.println(line);"
},
{
"type": "mcma",
"question": "Which of the following declaration(s) of object <code>readFromConsole</code> that allows you to read from <code>System.in</code>? (given with the correct package import)",
"options": [
"BufferedReader readFromConsole = new BufferedReader(new InputStreamReader(System.in));",
"Scanner readFromConsole = Scanner.readFrom(System.in);",
"readFromConsole = BuffereReader(System.in);",
"BufferedReader readFromConsole = new BufferedReader(new Scanner.readFrom(java.io.System.in));",
"Scanner readFromConsole = new Scanner(System.in);",
"readFromConsole = Scanner(System.in)"
],
"answer": [
0,
4
]
},
{
"type": "mc",
"question": "what is the expected output after running the main function?<br />\n<pre>public class Main\n{\n public static void main(String args[]){\n int a = 5;\n System.out.print(a);\n a += 2;\n System.out.print(a);\n a += 3;\n }\n}</pre>",
"options": [
"<pre>5\n7</pre>",
"<pre>10</pre>",
"<pre>7\n10</pre>",
"<pre>57</pre>",
"compile error"
],
"answer": 3
},
{
"type": "mc",
"question": "what is the expected output of the following program?<br />\n<pre>public class Main\n{\n public static void main(String args[]){\n int arr[] = { 1, 2, 3 };\n for(int i = 0; i < arr.length; ++i){\n System.out.print(arr[i]);\n }\n }\n}</pre>",
"options": [
"<pre>1\n2\n3</pre>",
"<pre>123</pre>",
"<pre>321</pre>",
"<pre>1 2 3</pre>",
"compile error"
],
"answer": 0
}
]
},
"2": {
"title": "Basic I/O - File",
"questions": [
{
"type": "mc",
"question": "Which of the following is the correct import statement for class <code>File</code>?",
"options": [
"import java/util/File",
"import java.util;",
"import java.util.File;",
"import util/File",
"import java.io.File;",
"import java/io/File",
"import java.util.file"
],
"answer": 4
},
{
"type": "fill",
"question": "given <pre>import java.io.File; // Import the File class\nimport java.io.FileNotFoundException; // Import this class to handle errors\nimport java.util.Scanner; // Import the Scanner class to read text files\n\npublic class ReadFile {\n public static void main(String[] args) {\n try {\n File myObj = new File(\"data.txt\");\n Scanner myReader = new Scanner(myObj);\n while (myReader.hasNextLine()) {\n String data = myReader.nextLine();\n ___________________;\n }\n myReader.close();\n } catch (FileNotFoundException e) {\n System.out.println(\"An error occurred.\");\n e.printStackTrace();\n }\n }\n}</pre>\nwhat should be filled in '______________' to print the whole file in console line-by-line?",
"answer": "System.out.println(data);"
}
]
}
},
"cpp": {},
"python": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment