Created
April 8, 2016 19:58
-
-
Save bhnascar/5281411edae20ccc698f446eb5988cde to your computer and use it in GitHub Desktop.
Trick questions involving Java methods...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MethodPractice | |
{ | |
public static int question1(int a, int b) { | |
return a * b; | |
} | |
public static void question2(int a) { | |
a = 2 * a; | |
} | |
public static void question3(int[] arr, int a) { | |
a = 2 * a; | |
arr[a] = 3; | |
} | |
public static void question4(String greeting) { | |
greeting += "Sophia"; | |
} | |
public static int question5(int[] arr) { | |
int minElement = arr[0]; | |
for (int i = 1; i < arr.length; i++) { | |
if (arr[i] < minElement) { | |
minElement = arr[i]; | |
} | |
} | |
return minElement; | |
} | |
public static void question6(int[][] arr) { | |
for (int r = 0; r < arr.length; r++) { | |
for (int c = 0; c < arr[0].length; c++) { | |
if (c == 1) { | |
arr[r][c] = 1; | |
} | |
} | |
} | |
} | |
public static void question7(int[][] arr) { | |
// Creating a new 2D array and pointing arr to that. | |
arr = new int[][]{{10, 20, 30}, | |
{40, 50, 60}, | |
{70, 80, 90}}; | |
} | |
public static void main(String[] args) { | |
// Okay here's a bunch of "what gets printed" | |
// questions... | |
// | |
// I'm pretty sure there's going to be | |
// "what gets printed" multiple-choice questions | |
// on the AP, where they give you a bunch of possible | |
// answers. So my questions are a wee-bit harder | |
// because I'm asking you to come up with the | |
// answer yourself rather than picking the right | |
// one from a list of options. :) | |
// | |
// Try doing it by hand first and when | |
// you've finished everything, then run this | |
// program with BlueJ (copy and paste the code) | |
// and check your answers. | |
// | |
// For the ones that you miss, try to figure out | |
// why you got it wrong. And then explain to me | |
// (on Sunday) what's really happening for those | |
// questions. | |
// | |
// Just so I can make sure you | |
// actually understand :) | |
// Question 1: | |
System.out.println("\n\nQuestion 1."); | |
int a = 5; | |
int b = 2; | |
int result = question1(a + b, a + b); | |
System.out.println(result); | |
// Question 2: | |
System.out.println("\n\nQuestion 2."); | |
a = 3; | |
question2(a); | |
System.out.println(a); | |
// Question 3: | |
System.out.println("\n\nQuestion 3."); | |
a = 1; | |
int[] arr = {10, 20, 30, 40, 50}; | |
question3(arr, a); | |
System.out.println(arr[a]); | |
System.out.println(arr[2]); | |
// Questin 4: | |
System.out.println("\n\nQuestion 4."); | |
String greeting = "Hi "; | |
question4(greeting); | |
System.out.println(greeting); | |
// Question 5: | |
System.out.println("\n\nQuestion 5."); | |
arr = new int[]{10, 20, 30, 40, 50}; // This just creates a new array with the numbers. | |
System.out.println(question5(arr)); | |
// Question 6: | |
System.out.println("\n\nQuestion 6."); | |
int[][] arr2 = {{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9}}; | |
question6(arr2); | |
print2DArray(arr2); // This just prints out the 2D array in square format. | |
// Question 7: | |
System.out.println("\n\nQuestion 7."); | |
arr2 = new int[][]{{1, 2, 3}, | |
{4, 5, 6}, | |
{7, 8, 9}}; | |
question7(arr2); | |
print2DArray(arr2); | |
} | |
/* Pretty-prints a 2D array of integers to the screen. */ | |
public static void print2DArray(int[][] arr) { | |
for (int i = 0; i < arr.length; i++) { | |
for (int j = 0; j < arr[i].length; j++) { | |
System.out.print(arr[i][j] + "\t"); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment