All-Combined Coding Questions
Which conditional operators would allow the loop to exit properly (the ?? location) ? public static void gameMenu() { Scanner scanner = new Scanner(System.in); char x = ' '; while(x ?? 'y') { System.out.print("End program (yes, no)"); x = scanner.nextLine().charAt(0); } scanner.close(); }
!=
Match the best answer with the question about this code: public class KnowledgeCheckTrek { public static final String TNG = "The Next Generation"; public static final String DS9 = "Deep Space Nine"; public static final String VOYAGER = "Voyager"; public static String trek(String character) { if (character != null && (character.equalsIgnoreCase("Picard") || character.equalsIgnoreCase("Data"))) { // IF ONE return TNG; } else if (character != null && character.contains("7")) { // IF TWO return VOYAGER; } else if ((character.contains("Quark") || character.contains("Odo"))) { // IF THREE return DS9; } return null; } public static void main(String[] args) { System.out.println(trek("Captain Picard")); System.out.println(trek("7 of Nine")); System.out.println(trek("Odo")); System.out.println(trek("Quark")); System.out.println(trek("Data").equalsIgnoreCase(TNG)); System.out.println(trek(null)); }}
(1) The number of times "The Next Generation" is printed = 0 (2) The number of times " character.equalsIgnoreCase("Data")" is called/executed = ?? Not: 1; 2; 0; 3 (3) The number of times "Deep Space Nine" is printed = 2 (4) The number of times "null" is printed = 1 (5) Which if statements will throw an error while running? = IF THREE
Given the bit of code, what is printed? int i = 1; String tmp = ""; while (i <= 0) { tmp += (i + ","); i = i + 1; } System.out.println(tmp.length());
0
Given the following code, what is printed? public static String formatterA(double val) { return String.format("%.2f%%,%2d", val, (int) val); } public static void main(String[] args) { System.out.println(formatterA(1/3.0)); }
0.33%, 0
Given the following binary number, what is the decimal value? 0001
1
You goal is to build a line of code prints the "Row" and "Seat" number for a student, given a whole number value that has been assigned to them. You can assume stuSeat is an int that has been validly set. You can also assume the max number of seats in a row is 20, and that all rows are the same length. You do not have to store the answer, so just print it out - as such, making it a single line of code. We can safely start counting at 0 for both rows and seats. p.s. You should draw this one out, if you don't get how it fully works. It is a deeper thinking question.
1) System.out.println("Row: " + 2) stuSeat / 20 3) + " Seat: " 4) + 5) stuSeat % 20 6) );
Given the following lines of code, assemble in the order that makes the most sense.
1) public class MyCoolProgram { 2) public static void main(String[] args) { 3) Scanner scanner = new Scanner(System.in); // place after main() before first print. 4) System.out.print("Step 1: Enter a width: "); 5) int width = scanner.nextInt(); 6) System.out.print("Step 2: Enter a height: "); 7) int height = scanner.nextInt(); 8) int area = width * height; 9) System.out.println("The area is " + area); 10) } }
Given the following lines of code, assemble in the order that makes the most sense.
1) public class MyCoolProgram { 2) public static void main(String[] args) { 3) String name; 4) name = "James Gosling"; 5) System.out.println("Hello " + name); 6) } }
Given the following memory tables (final result), and the following class - order the main method. public class KC{ private double myValue = 0; public void setValue(double myValue) { this.myValue = myValue; } public double getValue() { return myValue; }} Variable Value x 10 y 5.0 one @KC.one two @KC.two @KC.one variable value myValue 10.0 @KC.two variable value myValue 5.0
1) public static void main(String[] args) { 2) int x = 10; KC one = new KC(); KC two = new KC(); // placed on same line to make ordering easier 3) 4) one.setValue(y); 5) 6) 7) }
Given the following binary number, what is the decimal value? 1010
10
Given the bit of code, what is printed? int i = 1; String tmp = ""; while (i <= 6) { tmp += (i + ","); i = i + 1; } System.out.println(tmp.length());
12
Given the following binary number, what is the decimal value? 1111
15
Given the bit of code, what is printed? int i = 1; String tmp = ""; while (i <= 1) { tmp += (i + ","); i = i + 1; } System.out.println(tmp.length());
2
Given the following code, what is printed to the screen? public static int doItx2(int val) { return 2*val; } public static void doIt(int val){ System.out.print(val); } public static void main(String[] args) { doIt(doItx2(1)); doIt(5); System.out.print(doItx2(3)); }
256
Given the following code, what is printed?public static String formatterA(double val) {return String.format("%.2f%%,%2d", val, (int) val);}public static void main(String[] args) {System.out.println(formatterA(10/3.0));}
3.33%, 3
Given the following code, how many lines are printed? public static void loop() { for(int i = 0; i < 7; i+= 2) {System.out.println(i);}}
4
Given the following code, how many lines are printed? public static void loop() { for(int i = 0; i < 9; i+= 2) {System.out.println(i);}}
5
Given the following code, how many lines are printed? public static void loop() { for(int i = 0; i < 6; i+= 1) {System.out.println(i);}}
6
Given the following code, what is printed to the screen? public static int doItx2(int val) { return 2*val; } public static void doIt(int val){ System.out.print(val); } public static void main(String[] args) { doIt(doItx2(3)); doIt(1); System.out.print(doItx2(4)); }
618
Given the following code, how many lines are printed? public static void loop() { for(int i = 0; i < 7; i+= 1) { System.out.println(i); }}
7
Given the following code, what is printed? public static String stringAddition(String x, int y) { return x + y; } public static void main(String[] args) { System.out.println(stringAddition("A", 2)); }
A2
Given the following output, match the code generating it. Print 1: [Urianger, Alisaie] Print 2: [Urianger] Print 3: [Urianger, Thancred] Print 4: [Y'shtola, Urianger, Thancred, Yda]
ArrayList<String> list = new ArrayList<>(); 1) list.add("Urianger"); 2) list.add("Alisaie"); 3) System.out.println("Print 1: " + list); // line 3 4) list.remove(1); 5) System.out.println("Print 2: " + list); // line 5 6) list.add("Thancred"); 7) list.add("Minfilia"); 8) list.remove(2); System.out.println("Print 3: " + list); // on a single line to remove variability, yes this is allowed. 9) list.add(list.size() - 2, "Y'shtola"); 10) list.add("Yda"); System.out.println("Print 4: " + list);
Given the following code, what is printed? public static char characterAddition(char a, int b) { return (char) (a + b); } public static void main(String[] args) { System.out.println(characterAddition('A', 2)); }
C
Which line in the following code would cause a compile error: public class Hero { public static final String LEAGUE = "HERO"; // Option A public String powerLookup(int which) { String rtn = LEAGUE + ": Flight"; // Option B if (which < 0) { rtn = LEAGUE + ": Laser Eyes"; // Option C } return rtn; } public static void main(String[] args) { System.out.println(Hero.LEAGUE); // Option D Hero.LEAGUE = "Villain"; // Option F Hero ajax = new Hero(); System.out.println(ajax.powerLookup(-1)); } }
F
Given the following code, what is printed if printer("Go", "G0") is invoked? public static void printer() { printer("Start:"); } public static void printer(String value) { System.out.print(value); printer(value + "v2", value + 3); } public static void printer(String v1, String v2) { System.out.print(v1 + "," + v2); printer(3); } public static void printer(double val) { System.out.print(val / 2); }
Go,G01.5
Match the label with the correct part of the class: public class Cake { public static boolean EAT_IT = true; // Label 1 private String name; // Label 2 private double cost; public Cake() { // Label 3 this("Chocolate", 0.0); } public Cake(String name, double cost) { // Label 4 setName(name); setCost(cost); } // Label 5 public void setName(String str) { name = str; } public String getName() { return name; } public void setCost(double cost) { this.cost = cost; } public double getCost() { return this.cost; }}
LABEL 1 = ?? Not: Instance Variable; LABEL 2 = ?? Not: object; LABEL 3 = Default Constructor LABEL 4 = Overloaded Constructor LABEL 5 = Accessors and Mutator methods
Given the following code, match the label with the best descriptor of what that label is identifying. public class HelloWorld { // LABEL 1 /** // LABEL 2 * The main method is the entry point of the program, and the first thing called. * Start following the code from this method. * @param args these come from the comment line, and are unused in this program */ public static void main(String[] args) { // Ada is pretty cool // LABEL 3 String firstProgrammer; firstProgrammer = "Ada Lovelace"; // LABEL 4 System.out.println("Hello World"); System.out.print("Did you know that "); // LABEL 5 System.out.print(firstProgrammer); System.out.println(" was the worlds first programmer?"); }}
LABEL 1 = Class signature LABEL 2 = Start of multiline comment LABEL 3 = Single line comment LABEL 4 = Variable assignment LABEL 5 = Print to the console
Given the following code, match the label with the best descriptor of what that label is identifying. /** //LABEL 1 * <h1>Hello World Example</h1> * This is a basic example of a first program printing out to the terminal * * Example Available for Download at https://github.com/coloradostateuniversity/CSUCS1ClassExamples * * @author Albert Lionelle <br> * [email protected] <br> * Computer Science Department <br> * Colorado State University * @version 202010 */ public class HelloWorld { // LABEL 2 /** * The main method is the entry point of the program, and the first thing called. Start following the code * from this method. * @param args these come from the comment line, and are unused in this program */ public static void main(String[] args) { // LABEL 3 String firstProgrammer = "Ada Lovelace"; // LABEL 4 System.out.println("Hello World"); System.out.print("Did you know that "); System.out.print(firstProgrammer); System.out.println(" was the worlds first programmer?"); // LABEL 5 }}
LABEL 1 = Comment LABEL 2 = Class Signature LABEL 3 = Entry point of the program LABEL 4 = Variable declaration and assignment LABEL 5 = Print to the console with a new line added
Given the following code, what is printed? public static String formatterB(String str) { char tmp = str.charAt(0); char tmp2 = str.charAt(str.length()-1); return String.format("%S %s%2C%2c", str, str, tmp, tmp2); } public static void main(String[] args) { System.out.println(formatterB("Link")); }
LINK Link L k
Given the following code, match the label with the best definition public class Rectangle { int width = 0; // LABEL 1 int length = 0; public void setLength(int length) { // LABEL 2 this.length = length; } public void setWidth(int width) { this.width = width; } public int getArea() { return calcArea(width, length); } public static int calcArea(int width, int length) { // LABEL 3 return width*length; } public static void main(String[] args) { int length = 10; // LABEL 4 int width = 100; Rectangle smallBuildingOnCampus = new Rectangle(); // LABEL 5 smallBuildingOnCampus.setLength(length); smallBuildingOnCampus.setWidth(width); System.out.println(smallBuildingOnCampus.getArea()); }}
Label 1: instance variables Label 2: Instance Method Label 3: Static method Label 4: Local method variables Label 5: Constructs Object
Given the following code, what is printed if printer() is invoked? public static void printer() { printer("Start:"); } public static void printer(String value) { System.out.print(value); printer(value + "v2", value + 3); } public static void printer(String v1, String v2) { System.out.print(v1 + "," + v2); printer(3); } public static void printer(double val) { System.out.print(val / 2); }
Start:Start:v2,Start:31.5
Given the following code: public static String welcome(String basic, String greeting) { return greeting + ", " + basic; } And the following is printed to the screen: Welcome, Who Complete the statement below with the correct method call and parameters.
System.out.println( welcome("Who", "Welcome"));
Select the lines of code that will cause *true* to be printed public class KnowledgeChecksLovecraft { public static boolean isLovecraftian(String value, int cultists) { if (value != null && value.length() < 10) { if (value.equalsIgnoreCase("ghast") || value.equalsIgnoreCase("cthulhu") || value.equalsIgnoreCase("dagon") || value.equalsIgnoreCase("mi-go")) { return true; } else return cultists < 100 && cultists > 10; } else if (value != null && value.length() > 10) { return (value.equalsIgnoreCase("Kassogtha") || value.equalsIgnoreCase("Shoggoth") || value.equalsIgnoreCase("Azathoth") || value.equalsIgnoreCase("Y'golonac") || value.equalsIgnoreCase("Yog-Sothoth") || value.equalsIgnoreCase("Nyarlathotep")); } return false; } public static void main(String[] args) { // assume line is here } }
System.out.println(isLovecraftian("Unnamed", 20)); System.out.println(isLovecraftian("Nyarlathotep", 1000));
Select the lines of code that will cause *false* to be printed public class KnowledgeChecksLovecraft { public static boolean isLovecraftian(String value, int cultists) { if (value != null && value.length() < 10) { if (value.equalsIgnoreCase("ghast") || value.equalsIgnoreCase("cthulhu") || value.equalsIgnoreCase("dagon") || value.equalsIgnoreCase("mi-go")) { return true; } else return cultists < 100 && cultists > 10; } else if (value != null && value.length() > 10) { return (value.equalsIgnoreCase("Kassogtha") || value.equalsIgnoreCase("Shoggoth") || value.equalsIgnoreCase("Azathoth") || value.equalsIgnoreCase("Y'golonac") || value.equalsIgnoreCase("Yog-Sothoth") || value.equalsIgnoreCase("Nyarlathotep")); } return false; } public static void main(String[] args) { // assume line is here } }
System.out.println(isLovecraftian(null, 20));System.out.println(isLovecraftian("Beebo", 10));System.out.println(isLovecraftian("Dracula", 3));
Match the answers with the question that it best fits based on this code, assume the code has been fully run. public class Cake { public static boolean EAT_IT = true; private String name; private double cost; public Cake() { this("Chocolate", 0.0); } public Cake(String name, double cost) { setName(name); setCost(cost); } public void setName(String str) { name = str; } public String getName() { return name; } public void setCost(double cost) { this.cost = cost; } public double getCost() { return this.cost; } public static void main(String[] args) { Cake coconut = new Cake("Coconut", 1.0); Cake chocolate = new Cake(); double save = chocolate.getCost(); chocolate.setCost(coconut.getCost()); coconut.setCost(save); coconut.setName(chocolate.getName()+ " " +coconut.getName()); System.out.println(coconut.getCost()); System.out.println(coconut.getName()); } }
The cost of coconut cake when instantiated = 1 The starting name for chocolate = Chocolate The value in EAT_IT = true The cost of coconut cake = 0 The final line printed = Chocolate Coconut
Given the following problem. Fill in the questions, with the 'numeric' (no spaces) Answer. For example, the number days around the world is 79. Alice really hates matching socks after doing their laundry. Given that she only has three different types of socks, and that she can't look in the drawer before pulling out a sock, answer the following questions.
The minimum number of socks she would have to pull out of the drawer to find a matching pair is: 2, and the maximum number she would need to pull to always find a match is: 4.
Given the following code, match the answer with the correct question. public class KnowledgeChecks { public static final String SHELLEY = "Mary Shelley"; public static final String ALLAN = "Allan Rune Pettersson"; public static final String DOYLE = "Arthur Conan Doyle"; public static String nameTheAuthor(String novel, int year) { if (year <= 1900 && (novel.contains("Frankenstein") || novel.contains("Last")) || novel.contains("Falkner")) { return SHELLEY; } else if (novel.contains("Frankenstein")) { return ALLAN; } else if (novel.contains("Lost") || novel.contains("Scarlet")) return DOYLE; return null; } public static void main(String[] args) { System.out.println(nameTheAuthor("Frankenstein", 1818)); System.out.println(nameTheAuthor("The Last Man", 1826)); System.out.println(nameTheAuthor("The Lost World", 1912)); System.out.println(nameTheAuthor("Frankenstein's Aunt", 1900)); System.out.println(nameTheAuthor("A Study in Scarlet", 1886)); } }
The number of times "Allan Rune Pettersson" is printed = 0 The number of times "Arthur Conan Doyle" is printed = 2 The number of times "Mary Shelley" is printed = 3
Is the following code valid? public class TermPair { public final Integer term; public final String name; public TermPair(int term, String name) { this.term = term; this.name = name; } }
True
Group code into valid or invalid - invalid can include both code that doesn't compile, and code that compiles but would return a runtime error.
Valid: String clara = "Raven" + '_' + "nevermore"; int nevermore = 'R' + 'a' + 'v' + 'e' + 'n'; char x = "Raven".charAt(0); int val = 'x' + 2; char x = 67; Invalid: String nevermore = "Raven"; char x = nevermore.charAt(nevermore.length()); String number = 10 + 'x'; int number = 10 + "x"; string stich = "lilo"; String joe = 'joe';
Given the following items, categorize the code as valid, or invalid (won't compile). Just assume as single line (don't make assumptions about other lines in a total program)
Valid: String w = "world"; System.out.println("Hello " + w); System.out.println("Hello world"); String name = "Ada"; Invalid: String w = "world" System.out.println("Hello " + world); /* this is a comment // end comment System.out.println(Hello World); String name = "Ada" print("hello world");
Given the following blocks of code, categorize them as valid (they will compile) or invalid (they will not compile)
Valid: int val = 0; double val2 = val; double puppyCost = 100; String name = "Spot!"; int puppyCounter = 0; int val1 = 10, val2 = 5; int val1, val2, val3; double _PI = 3.14; String dbl = " "; char s = 'S'; Invalid: double val = 1.1; int val3 = val; int val =1, double val2 = 2.0; char shorter = "S"; int $counter$ = 0 String single = ' '; int counter = 1.1; int new = 10; int 3val = 3;
Mark the following code as valid or not:
Valid: public double calcArea(double w, double h) {} public static void main(String[] args) { } public void doSomething() {} Invalid: public static void $doSomething(int, double) {} Public static void doAnotherSomething() {} public static void main(String[] args);
Given the following code. Mark the statements that are not reached / printed ! public static void printIt(int value) { if(value <= 10) { if (value > 0) { System.out.println("Play a paladin"); } else { System.out.println("Play a white mage"); } } else if (value < 15) { System.out.println("Play a black mage"); } else { System.out.println("Play a monk"); } } public static void main(String[] args) { printIt(5); printIt(10); printIt(15); }
[] Play a Black mage [] Play a White mage
Given the following code. Mark the statements that are not reached / printed ! public static void printIt(int value) { if(value <= 10) { if (value > 0) { System.out.println("Play a paladin"); }else { System.out.println("Play a white mage"); } } else if (value <= 15) { System.out.println("Play a black mage"); } else { System.out.println("Play a monk"); } } public static void main(String[] args) { printIt(5); printIt(10); printIt(15); }
[] Play a White mage [] Play a monk
Given the string String spell = "Redikulus"; How would you find the index of the 'u' just before the last 's' using a single line of code? Multiple answers may be correct.
[] spell.indexOf('u', spell.length() - 2); [] spell.indexOf('u', spell.indexOf('l')); [] spell.lastIndexOf('u');
What is the value of shorter? (be exact, don't add any spaces by accident)String magic = "abracadabra";String shorter = magic.substring(magic.indexOf("b"),magic.indexOf("a", magic.indexOf("c"))+1);
braca
Write a Java statement that declares a variable named hoursSlept of type double, and initializes its value to 3.21. Do not forget the semi-colon. Follow basic java syntax in using proper spaces - else it won't be graded correctly.
double hoursSlept = 3.21;
Given the output for the given code, match the correct conditional operator needed in location ?? public static boolean signCheck(int val) { return 10 ?? val; } public static void main(String[] args) { System.out.printf("%b, %b, %b", signCheck(10), signCheck(0), signCheck(-10)); }
false, false, false: < true, true, true: >= false, true, true: > true, false, false: ==
Write a Java statement that declares a variable named hoursSlept of type int, and initializes its value to 314159. Do not forget the semi-colon. Follow basic java syntax in using proper spaces - else it won't be graded correctly.
int hoursSlept = 314159;
Given the following code, match the output with the print statement. public static String overloaded(int x) { return overloaded(x, "answer"); } public static String overloaded(int x, String str) { return "The " + str + " is " + x; }
overloaded(42) = The answer is 42 overloaded(42, "question") = The question is 42
I want to create a static variable that is both public and read only. Order the following bit of code to accomplish that task. Assume standard ordering / best practices.
public static final int MY_VALUE = 10;
Given the following output, order the code:For loopY'shtolaMinfiliaYdaEnd loop with a size of 3List size of 2Y'shtolaYdaAlisaieUriangerThancred
public static void main(String[] args) { 1) ArrayList<String> list = new ArrayList<>(); 2) list.add("Y'shtola"); 3) list.add("Minfilia"); 4) list.add("Yda"); 5) System.out.println("For loop"); // place immediately above for loop 6) for (int i = 0; i < list.size(); i++) { 7) String name = list.get(i); 8) System.out.println(name); 9) } // end for 10) System.out.printf("End loop with a size of %d\n", list.size()); 11) list.remove(1); 12) System.out.printf("\n\nList size of %d\n", list.size()); 13) list.add("Alisaie"); 14) list.add("Urianger"); 15) list.add("Thancred"); 16) for (String name2 : list) { 17) System.out.println(name2); 18) } // end foreach }
What is the value of knock? (be exact, don't add any spaces by accident) String knocker = "tattarrattat"; String knock = knocker.substring(knocker.indexOf("r")+1, knocker.indexOf("t", knocker.length()-1)+1);
rattat
Given the following code, match the code with the correct output public static String sub(String str) { return str.substring(str.indexOf(",") + 1, str.indexOf(",", str.indexOf(",") + 1)); } public static String sub2(String str) { return str.substring(str.indexOf("@")); } public static String sub3(String str) { String rtn = ""; String key = "KitHawk"; String key2 = "abcdefgh"; rtn += key2.charAt(key.indexOf(str.charAt(0))); rtn += key2.charAt(key.indexOf(str.charAt(1))); rtn += key2.charAt(key.indexOf(str.charAt(2))); return rtn; }
sub("Brad,Magenta,Columbia") = Magenta sub2("[email protected]") = @rhps.com sub3("Kakwit") = aeg sub("Magenta,Brad,,Columbia") = Brad sub2("frankie@@rhps.com") = @@rhps.com sub3("kaKwiH") = gea
Given the output for the given code, match the correct conditional operator needed in location ?? public static boolean signCheck(int val) { return 0 ?? val; } public static void main(String[] args) { System.out.printf("%b, %b, %b", signCheck(10), signCheck(0), signCheck(-10)); }
true, false, false: < false, true, false: == false, true, true: >= false, false, true: > true, false, true: !=