Extra Questions on Arrays_practise_answers.pdf - Extra Questions on Arrays(single and 2D and methods These questions are not available on Codio Rather

# Extra Questions on Arrays_practise_answers.pdf - Extra...

• Test Prep
• 7

This preview shows page 1 - 3 out of 7 pages.

Extra Questions on Arrays (single and 2D) and methods. These questions are not available on Codio. Rather, you will need to use another IDE to write and test the code. These are practise questions only – they are NOT the questions that will be on the practicum. Question1 In your main method, use a Scanner object to first read in one int (n) that represents the size of an array of Strings, followed by a set of (n) Strings on one line. There will be one final String which will contain a single character. Go through the array and print all the Strings that begin with that character (upper or lower case) (no need to create a method for this – just do it in the main method). For example: Sample Input Sample Output 3 Canada calculate divide c 4 dog cat bird snake S Canada calculate snake Code: import java.util.Scanner; public class ArrayOfStrings { public static void main (String []args ){ //create Scanner to read in size of array and set of Strings Scanner kb = new Scanner (System.in); int size = kb.nextInt(); String [] arr = new String [size]; //add Strings to the array by reading Strings from Scanner object for (int i=0;i<size;i++) arr[i]=kb.next(); /*read in the String and get the char that will be used to go through the array to print out Strings that start with the char*/ char ch = kb.next().charAt(0); //go through array and print word if it starts with char for (int i=0; i<arr.length; i++){ if (arr[i].toLowerCase().charAt(0)==ch || arr[i].toUpperCase().charAt(0)==ch) System.out.print(arr[i] + " "); } } }
Question2 In your main method, use a Scanner object to first read in one int (n) that represents the size of an array of doubles, followed by a set of (n) doubles on one line. Go through the array and print every other element, starting at 0. (no need to create a method for this – just do it in the main method). Be careful of out bound errors. For example: Sample Input Sample Output 3 1 2 3.5 6 2.2 3 4 5.6 3.4 1.2 1.0 3.5 2.2 4.0 3.4 Code: import java.util.Scanner; public class ArrayOfDouble { public static void main (String []args ){ //create Scanner to read in size of array and set of double values Scanner kb = new Scanner (System.in); int size = kb.nextInt(); double [] arr = new double [size]; //add numbers to the array by reading doubles from Scanner object for (int i=0;i<size;i++) arr[i]=kb.nextDouble(); //print out every other element in the array starting at 0 for (int i=0;i<arr.length; i++) if (i%2==0) //will print out every other 'even' indice System.out.print(arr[i] + " "); } } Question3 Suppose you had a company with 10 employees and you stored their names in an array. Then if you hired 5 new employees, you would need to make a new array of size 15 (10 + 5) and copy over the original employees before being able to add the new employees to the array.

#### You've reached the end of your free preview.

Want to read all 7 pages?

• Fall '12
• MacKay

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern