290 likes | 420 Views
Rules. Two Teams Questions worth 1-3 points Entire team can confer to answer the question Max of 2 minutes per question You can use a computer on any question except “what does this program output…” Teams switch when either Three questions have been asked or
E N D
Rules • Two Teams • Questions worth 1-3 points • Entire team can confer to answer the question • Max of 2 minutes per question • You can use a computer on any question except “what does this program output…” • Teams switch when either • Three questions have been asked or • A team answers incorrectly three times (three strikes) • After three strikes, the opposing team can “steal” the points by answering the question correctly • Team with the most points at the end wins
Question 1 of 12 • Name at least two ways a List is more flexible than an array.
Answer 1 (2 points) • Name at least two ways a List is more flexible than an array. • Don’t have to specify the size of the List, can add items to the end • Can remove items at a specified index • Built-in methods to search the list
Question 2 (3 points) • What is the output of this code? int[] a = { 0, 1, 1, 0, 2, 1 }; int[] h = new int[3]; h[0] = 0; h[1] = 0; h[2] = 0; for (int i = 0; i < a.Length; i++) { h[a[i]]++; } Console.WriteLine(h[0]); Console.WriteLine(h[1]); Console.WriteLine(h[2]);
Answer 2 • 2 • 3 • 1
Question 3 (1 point) • If a method is declared to be virtual then what can we do with that method in a derived class?
Answer 3 • Override it
Question 4 (1 point) • Write code that creates an instance of the Dude class using the constructor (send in any data you like that is valid) class Dude { private string name; private int age; public Dude(string n, int a) { name = n; age = a; } }
Answer 4 • Dude dude = new Dude("Ben Curtis", 29);
Question 5 (3 point) • Write equivalent code that uses two while loops instead of two for loops for (inti = 0; i < 10; i++) { for (int j = 0; j < i; j++) { int temp = ary[j]; ary[j] = ary[i]; ary[i] = temp; } }
Answer 5 • Code inti = 0; while (i < 10) { int j = 0; while (j < i) { int temp = ary[j]; ary[j] = ary[i]; ary[i] = temp; j++; } i++; }
Question 6 (2 points) • What is the difference between a reference type and a primitive type?
Answer 6 • Primitive type stores the actual value directly in the memory location for the variable • Reference type stores an address in memory, or a reference, to another place in memory that stores the actual value of the data
Question 7 (2 points) • Give the code to create a variable named “nums” that is a List of doubles, and then add 3.5 and 10.4 to the list
Answer 7 • List<double> nums = new List<double>(); • nums.Items.Add(3.5); • nums.Items.Add(10.4);
Question 8 (1 point) • What is the difference between public, private, and protected?
Answer 8 • Public: accessible from outside the class • Private: accessible only inside the class • Protected: accessible inside the class and in any derived classes
Question 9 (3 points) • Write a class named “AfricanGrey” that is derived from Parrot and override description() to return “Grey with red tails” class Parrot { public virtual string description() { return ("Talking Bird"); } }
Answer 9 • Code class AfricanGrey: Parrot { public override string description() { return ("Grey with Red Tails"); } }
Question 10 (1 point) • How is it that inheritance helps us eliminate redundant code?
Answer 10 • Methods or variables can be defined in parent classes and are “inherited” by any derived class, so we don’t have to write the inherited code again
Question 11 (2 points) • Fill in the blanks so the method accepts an array of strings and a target string, and also invokes the method public boolsearch( __________ ary, string targetname) { for (int i = 0; i < ary.Length; i++) { if (ary[i].Equals(targetname)) return true; } return false; } public void button_click() { string[] ary = { "Joe", "Bob", "Ted" }; if (search( _______ , "Ted")) { MessageBox.Show ("Ted is in the array."); } }
Answer 11 • Code public boolsearch( string[] ary, string targetname) { for (int i = 0; i < ary.Length; i++) { if (ary[i].Equals(targetname)) return true; } return false; } public void button_click() { string[] ary = { "Joe", "Bob", "Ted" }; if (search( ary, "Ted")) { MessageBox.Show ("Ted is in the array."); } }
Question 12 (3 points) • Give the output private void question12(string[] a, int count) { a[2] = ""; count--; } private void button6_Click(object sender, EventArgs e) { string[] ary = { "Joe", "Bob", "Ted" }; int count = 3; question12(ary, count); Console.WriteLine("Count: " + count); for (inti = 0; i < count; i++) { Console.WriteLine(ary[i]); } }
Answer 12 • Count = 3 • Joe • Bob • <blank>
Runoff Question • Write the Money class so the following code works: Money dinero = new Money(10000, "pesos"); Money cash = new Money(5, "dollars"); // Outputs 10000 pesos MessageBox.Show(dinero.Amount+ " " + dinero.Currency); // Outputs 5 dollars MessageBox.Show(cash.Amount+ " " + cash.Currency);
Answer class Money { private int amount; public int Amount { get { return amount; } set { amount = value; } } private string currency; public string Currency { get { return currency; } set { currency = value; } } public Money(int a, string c) { amount = a; currency = c; } }