ChatGPT quiz for 1322

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Can a private member of a class be accessed by code outside of the class?

no

Which of the following is an example of a public member in C#?

public string Name {get; set;}

Which keyword is used to indicate that a parameter is passed by reference in C#?

ref

What is recursion?

A programming technique that involves a function calling itself

An object is created with a new keyword?

At run time

OOP feature which derives a class from another class

Inheritance

What is the default way of passing parameters in C#?

By value

Which of the following is an example of tail recursion?

Calculating the factorial of a number

Which of the following problems is well-suited for a recursive solution?

Calculating the sum of a series of integers

What is the output of the following code? class A { public virtual void do_it() { Console.WriteLine("A"); } } class B : A { public override void do_it() { Console.WriteLine("B"); } } class MainClass { public static void Main (string[] args) { A myClass=new B(); myClass.do_it(); } }

B

using System; class MainClass { public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1; } public static void Main (string[] args) { try { do_stuff(); Console.WriteLine("A"); } catch(Exception e) { Console.WriteLine("B"); } } }

B

What is the maximum number of times a recursive function can call itself in C#?

It depends on the size of the call stack and available memory

Which of the following is a benefit of passing by reference in C#?

It's faster than passing by value

If you call do_stuff() from main, what if any exception will you see? public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1; }

Java: ArrayIndexOutOfBoundsException C#: IndexOutOfRangeException

What is the difference between passing by value and passing by reference in C#?

Passing by value means passing a copy of the value to a method, while passing by reference means passing the address of the value to a method.

What is the main difference between protected and private access modifiers in C#?

Protected members can be accessed within the same class and any derived classes, while private members can only be accessed within the same class.

What is the default access modifier in C#?

private

Which access modifier is used to make a member accessible only within the class in which it is defined?

private

The code within the finally block is always executed, no matter how the program flow leaves the try block. This guarantees that the finally block will be executed even if an exception is thrown.

True

The try-catch construct consists of one try block and one or more catch blocks. The try block contains the code that could throw exceptions.

True

Assume you have a file called a.txt with the following content : jane,100 tim,80 janet,40 What is the output of the following code: using System; using System.Collections.Generic; using System.IO; class Score { public string name; public int theScore; public Score(string name, int theScore) { this.name=name; this.theScore=theScore; } } class MainClass { public static void Main (string[] args) { List<Score> scoreCard = new List<Score>(); try { StreamReader sf = new StreamReader("a.txt"); while(! sf.EndOfStream) { string line=sf.ReadLine(); string[] parts=line.Split(","); Score newScore = new Score(parts[0],Int32.Parse(parts[1])); scoreCard.Add(newScore); } } catch(IOException e) { Console.WriteLine(e.Message); } Console.WriteLine(scoreCard[1].name); } }

tim

A method with a return type of void may still have a return statement in it's code.

true

An exception is a notification that something interrupts the normal program execution. Exceptions provide a programming paradigm for detecting and reacting to unexpected events.

true

Two methods that have the same name, but different parameters are said to be overloaded methods.

true

Assume you have a file called QuizGrades.csv with the following content: Student1,100,90,80,90,80 Student2,70,80,85,85,80 Student3,100,100,100,95,100 Student4,70,70,80,70,80 Student5,90,90,90,90,100 Which of the following code snips correctly reads each line from the file correctly assigning it to a variable called line?

while(! myScan.EndOfStream) { string line=myScan.ReadLine(); }

What is the main difference between public and private access modifiers in C#?

Public members can be accessed by any code in the program, while private members can only be accessed within the same class.

Which of the following is a disadvantage of using recursion?

Recursion is more difficult to understand and debug than iterative solutions

Which of the following data structures is often used in recursive algorithms?

Stacks

Assume you have a file called QuizGrades.csv with the following content: Student1,100,90,80,90,80 Student2,70,80,85,85,80 Student3,100,100,100,95,100 Student4,70,70,80,70,80 Student5,90,90,90,90,100 Which of the following lines is the correct way to open a text file such as the above for reading?

StreamReader myScan= new StreamReader("QuizGrades.csv");

What is a base case in recursion?

The condition that terminates the recursion by returning a value without calling the function again

using System; class Wrong : Exception { public Wrong(String message):base(message) {} } class MainClass { public static void doStuff(int x) { if(x<0) { throw new Wrong("Too Low"); } else if(x==0) { Console.WriteLine(x); } else { doStuff(x-1); Console.WriteLine(x); } } public static void Main (string[] args) { try { doStuff(3); Console.WriteLine("---"); doStuff(-1); } catch(Exception e) { Console.WriteLine(e.Message); } } }

0 1 2 3 ... Too Low

What is the output of the following recursive function when called with n = 4? void CountDown(int n) { if (n == 0) { Console.WriteLine("Blastoff!"); } else { Console.WriteLine(n); CountDown(n - 1); } }

4, 3, 2, 1, Blastoff!

What is the output of the following code? using System.Collections; class MainClass { public static void Main (string[] args) { ArrayList myNumbers = new ArrayList(); myNumbers.Add(10); myNumbers.Add(20); myNumbers.Add(30); myNumbers.Add(40); Console.WriteLine(myNumbers[myNumbers.Count-1]); } }

40

What is the output of the following code: using System; class MainClass { public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1; } public static void Main (string[] args) { try { do_stuff(); } catch(IndexOutOfRangeException e) { Console.WriteLine("A"); } catch(SystemException e) { Console.WriteLine("B"); } catch(Exception e) { Console.WriteLine("C"); } } }

A

What is the output of the following code: using System; class MainClass { public static void do_stuff() { int[] myNums = new int[5]; myNums[5]=1; } public static void Main (string[] args) { try { do_stuff(); } catch(Exception e) { Console.WriteLine("A"); } finally { Console.WriteLine("X"); } } }

A X

What is tail recursion

A recursive function in which the final result is calculated in the last call to the function

What is memoization in recursion?

A technique that involves storing the results of expensive function calls and returning the cached result when the same inputs occur again

Which of the following statements is true?

A try/catch block should be used when reading and writing to a file.

After executing the following code, what will be the contents of A.txt? using System; using System.IO; class MainClass { public static void write_file(string filename, string line, int x) { try { StreamWriter theFile = new StreamWriter(filename); for(int i=0;i<x;i++) { theFile.WriteLine(line); } theFile.Close(); } catch(IOException e) { Console.WriteLine("Error reading file: "+e.Message); } } public static void Main (string[] args) { write_file("A.txt","Hi",3); write_file("A.txt","Hi",3); } }

A.txt will contain the word Hi on three lines

using System; using System.IO; class MainClass { public static void write_file(string filename, string line, int x) { try { StreamWriter theFile = new StreamWriter(filename); for(int i=0;i<x;i++) { theFile.WriteLine(line); } theFile.Close(); } catch(IOException e) { Console.WriteLine("Error reading file: "+e.Message); } } public static void Main (string[] args) { write_file("A.txt","Hi",3); } }

A.txt will contain the word Hi on three lines

What is the difference between direct recursion and indirect recursion?

Direct recursion involves a function calling itself, while indirect recursion involves two or more functions calling each other in a cycle.

The Socket object in Java, and the TCPClient object in C# can be used to read information from a remote server, but not write to it?

False

Can a method have both pass by value and pass by reference parameters in C#?

Yes, a method can have both pass by value and pass by reference parameters.

Which of the following data types can be passed by reference in C#?

both value types and reference types

Which of the following is an example of passing by reference in C#?

int x = 5; SomeMethod(ref x);

Which of the following is an example of passing by value in C#?

int x = 5; SomeMethod(x);

Which of the following is an example of a private member in C#?

private bool isDone;

Which access modifier is used to make a member accessible within the same class and any derived classes?

protected

Which access modifier is used to make a member accessible to code outside of the class in which it is defined?

public

What happens to a parameter passed by reference when it is modified inside a method in C#?

the original value is modified

Which of the following will successfully write the word Output to the file a.txt in the current working directory?

using System; using System.IO; class MainClass { public static void Main (string[] args) { try { StreamWriter sr = new StreamWriter("a.txt"); sr.WriteLine("Output"); sr.Close(); } catch(IOException e) { Console.WriteLine(e.Message); } } }


Ensembles d'études connexes

BUS 359 Consumer Behavior Chapter 1

View Set

ECON Money and Banking (Chapter 14)

View Set

Chapter 7: Cell structures and Function Study for Finals

View Set

Operations and Supply Chain Management

View Set