CS3500 Final Quiz Questions
UDP and TCP are both networking protocols that allow processes running on different machines to communicate. How are they different? 1. Which protocol does not guarantee that the recipient will receive packets from the sender? 2. Which protocol is likely to result in less network traffic even when sending the same amount of data? Hint: think about what guarantees one of the protocols makes, and how those guarantees are accomplished. 3. Which protocol will we use in our game?
1. UDP 2. UDP 3. TCP
Assume some table T1 has 5 rows, and some table T2 has 3 rows. How many rows are in the result of the following query? SELECT * FROM T1 JOIN T2;
15
Consider a database for a university that represents students and classes. For simplicity, the database only needs to store the following information: Details of each student, such as their name, ID, and major Details of each class, such as its name, number, and syllabus. Assume the database is reset each semester, and we do not need to worry about multiple offerings of the same class. Details about which student(s) are enrolled in which class(es), where a student can enroll in any number of classes and a class can have any number of students enrolled in it. With the information given, and considering the design principles discussed in class, how many tables should this database use to represent the information?
3
Consider the upgraded Snake server that runs a web server concurrently with the game server. Suppose there is one game client connected (and thus one socket for it), and each TcpListener also has its own socket. If a web browser has connected to http://localhost and finished loading a web page, how many total sockets are open on the server? Note: this is asking about server-side sockets only.
3
If two clients are connected to the server, how many event loops (of any kind) are running on the server program? Remember that event loops are not just for receiving.
3
Consider this attempted implementation of the ReceiveCallback method from PS7: 1 private static void ReceiveCallback(IAsyncResult ar){ 2 SocketState state = (SocketState)ar.AsyncState; 3 Socket socket = state.TheSocket;try{ 4 int bytesRead = socket.EndReceive(ar);... }catch (Exception e){...} 5 state.OnNetworkAction(state); 6 state.TheSocket.BeginReceive(state.buffer, 0, SocketState.BufferSize, 0, ReceiveCallback, state); } Which line(s) do not belong in this method? Select all that apply. 1 2 3 4 5 6
6
Suppose four C# programs are running: a server, and three clients connected to the server using TCP sockets. The server has closed its TCPListener, and is no longer accepting additional clients, but the three already connected remain connected. How many total C# Socket objects are there among the four programs?
6
Consider the following C# code in a MAUI Application: 1 private void ButtonClick(object sender, EventArgs e){ 3 x++; 4 Thread t = new Thread(WorkerMethod); 5 t.Start(); 6 x--; 7 } 8 private void WorkerMethod(){ 9 x *= 2; 10 // some long running work that does not use x 11 x /= 2; 12 } Assume: The application has a button, and ButtonClick is registered as the handler for the button's click event. The application also has an int field called x. The button is only clicked once. Which line(s) in the above code are part of a critical section? Select all that apply. 3 4 6 9 10 11
6, 9, 11
Assume the right algorithms and data structures are already being used. Which of the following are reasons for the number one rule of optimizing? Select all that apply. It is not possible for a programmer to perform mirco-optimizations that the compiler can't already do Most micro-optimizations are already performed by the compiler If unoptimized code is not causing any performance problems, then it is better to leave it alone since micro-optimizations can make the code less readable There is no way to optimize code without changing its correctness
Most micro-optimizations are already performed by the compiler If unoptimized code is not causing any performance problems, then it is better to leave it alone since micro-optimizations can make the code less readable
Consider the following C# method: 1 void TryDequeue(){ 2 int numLeft = theQ.Count; 3 if(numLeft < 2){ 4 message = "not enough items"; 5 } 6 else 7 theQ.Dequeue(); 8 } Assume: theQ is a Queue that is shared by multiple concurrent threads message is a string that is shared by multiple concurrent threads The method's purpose is to try to remove an item from the queue, but only if it's not the last item. If the queue is already empty or only has one item, it sets message to indicate this. The code is full of potential race conditions. Which of these possible modifications would correctly project against the race conditions? Note: we are not necessarily concerned with solutions that are a bad design, as long as they prevent race conditions. Assume there are no other blocks of code that use nested locks. Select all that apply. Put lock(theQ) around line 2, put lock(message) around line 4, and put lock(theQ) around line 7 Put lock(theQ) around lines 2-7, and put lock(message) around line 4 Create a new variable 'x' to represent a shared key for theQ and message, and put lock(x) around lines 2-7, then change all other critical sections involving theQ or message to lock using 'x' Put lock(theQ) around lines 3-7, and put lock(message) around line 4 Which of the choices is a safer design (even if there are multiple correct choices)? The 1st choice The 2nd choice The 3rd choice The 4th choice
Put lock(theQ) around lines 2-7, and put lock(message) around line 4 Create a new variable 'x' to represent a shared key for theQ and message, and put lock(x) around lines 2-7, then change all other critical sections involving theQ or message to lock using 'x' The 3rd choice
Moore's law predicts that roughly every two years, _________ This implies that software we write today will _____________ if run on a new computer in two years.
1. Transistor density on processors will double . 2. Not necessarily run faster
On a host machine at a particular address, what identifies the process within that machine that a network communication is destined for?
port
Suppose you point your web browser to https://www.google.com/?somevar=this%20or%20that What is the plain text version of the value of "somevar" after the web server decodes it?
this or that %20 is a space
Suppose you point your web browser to https://www.cs.utah.edu/soc-news Fill in the blank below in the HTTP request sent to the server by your browser. GET _____ HTTP/1.1
/soc-news/
Which of the following is the best way to represent relationships between different types of entities in a relational database? Store the two types of entities in one relation where a row represents the two related entities Store the different types of entities in separate relations and create an additional relation that stores one full tuple from each related entity as a row Store the different types of entities in separate relations and create an additional relation that stores a key field or field(s) for each related entity as a row
Store the different types of entities in separate relations and create an additional relation that stores a key field or field(s) for each related entity as a row
Consider the below C# program. static void Worker1(){ Thread t2 = new Thread(Worker2); t2.Start(); Thread.Sleep(1000); Console.Write("b"); } static void Worker2() { Console.Write("c"); } static void Main(string[] args){ Thread t = new Thread(Worker1); Console.Write("a"); t.Start(); } Assume two changes to this code: the last two statements in main are swapped the Sleep statement is replaced with t2.Join(); Which of the following are possible outputs of this program? abc acb bac bca cab cba
acb cab cba
Consider the following C# code in a MAUI Application: private void ButtonClick(object sender, EventArgs e){ // blank line 1 Thread t = new Thread(WorkerMethod); t.Start(); // blank line 2 } private void WorkerMethod(){ // blank line 3 // some long running work // blank line 4 } Assume: The application has a button, and ButtonClick is registered as the handler for the button's click event. The application also has a label called status. Suppose we want to set the status label so that it displays "working" while the worker thread is running, and then set it back to "ready" when it's done. We would do this by running: status.Text = "working"; and status.Text = "ready"; On which lines in the code above would we change the text of the label? Set the text to "working" on line: ________ Set the text to "ready" on line: _________ True or false: setting the text to "ready" must be done via a Dispatcher:
blank line 1 blank line 4 true
Suppose some program uses three objects as keys for mutex locks (a, b, and c). Thread 1 and Thread 2 execute the following blocks of code concurrently: /* Thread 1 */ lock(a){ lock(c){ } } lock(b){} /* Thread 2 */ lock(b) { lock(a) { lock(c) { } } } This program results in deadlock: always sometimes never
never
Consider the following C# code snippets. Assume all snippets start execution in their Foo methods. Snippet1: void Foo(){ while(...){ s.BeginReceive(..., OnReceive); } }.. .void OnReceive(IAsyncResult ar){ // other code that doesn't start a receive // process the received data } Snippet2: void Foo(){ s.BeginReceive(..., OnReceive); }... void OnReceive(IAsyncResult ar){ // other code that doesn't start a receive // process the received data s.BeginReceive(..., OnReceive); } Snippet3: void Foo(){ s.BeginReceive(..., OnReceive); }... void OnReceive(IAsyncResult ar){ // other code that doesn't start a receive s.BeginReceive(..., OnReceive); // process the received data } Most of the parameters to BeginReceive have been left out for simplicity, but assume they are valid. Assume s is a Socket object that has a valid connection, and both methods have access to it. 1. In Snippet1, how many threads could be processing some received data concurrently? 2. In Snippet2, how many threads could be processing some received data concurrently? 3. In Snippet3, how many threads could be processing some received data concurrently? 4. Which of the three snippets implements a proper event loop? 5. Assume the code in the question is server-side code. How many client connections does each snippet represent? 6. All of the code snippets are missing an important operation on the line: // other code that doesn't start a receive Enter the name of the method that must be called on that line in order to be able to start processing the data. Do not actually call the method on a particular object, and don't provide parameters or parentheses, just give the exact name of the method.
1. A theoretically unlimited number 2. one 3. A theoretically unlimited number 4. Snippet2 5. Snippet1: A theoretically unlimited nmber Snippet2: 1 Snippet3: 1 6. EndReceive
In an MVC system involving asynchronous updates from a server, the View (containing UI elements) should provide the Controller information via __________________________________ , and the Controller should provide the View information via __________________.
1. Having a reference to the Controller and invoking Controller method(s) 2. Correct!An event/delegate
Consider the following C# code: class Server{ byte[] messageBuffer = new byte[50]; Socket s; ... Foo(){ s.BeginReceive(messageBuffer, 0, 50, SocketFlags.None,OnReceive, null); } private void OnReceive(IAsyncResult ar { s.EndReceive(ar); s.BeginReceive(messageBuffer, 0, 50, SocketFlags.None, OnReceive, null); } } Assume that by the time Foo is invoked, the Socket s has been initialized to a valid Socket and is connected to a client. At some point after that, the client performs three send operations consisting of 10 bytes each. How many times is OnReceive invoked? 0 1 2 3 4 An unknown number of times
An unknown number of times
Fill in the blanks in the C# code below so that it represents an SQL query on the Library database to find the row from the Titles table representing the book called Dune. Assume theCommand is a valid MySqlCommand object. theCommand./*BLANK 1*/ = "SELECT * FROM Titles WHERE Title = /*BLANK 2*/"; Replace /*BLANK 1*/ with: Replace /*BLANK 2*/ with:
Answer 1:Correct!CommandText Answer 2:Correct!'Dune'
Which of the following statements are true about sampling performance profilers? Select all that apply. They measure exactly how much time each part of your program took Assuming you have picked the right algorithms, data structures, and language, using a profiler is the first thing you should do if your software is not running fast enough They must employ advanced sampling techniques to avoid aliasing Aliasing is not a problem in C# due to the unpredictability that the runtime introduces A profiler tells you what micro-optimizations should be made to your code to improve performance
Assuming you have picked the right algorithms, data structures, and language, using a profiler is the first thing you should do if your software is not running fast enough They must employ advanced sampling techniques to avoid aliasing Aliasing is not a problem in C# due to the unpredictability that the runtime introduces
If runtime performance is the top priority of a software project, which of the following should you do? Select all that apply. Use a convenient language such as C# to reduce programmer effort Carefully pick the right algorithms and data structures Once it is working, use a profiler to identify where mirco-optimizations are needed Understand what micro-optimizations the compiler is and is not capable of performing
Carefully pick the right algorithms and data structures Once it is working, use a profiler to identify where mirco-optimizations are needed Understand what micro-optimizations the compiler is and is not capable of performing
Consider the following C# code: class Thing{ static HashSet<int> vals = new HashSet<int>(); void AddStuff(){ lock(vals){ vals.Add(5); } } void RemoveStuff(){ lock(vals){ vals.Remove(5); } vals.Add(3); } } Suppose there are two threads, T1 and T2. T1 is running AddStuff, and T2 is running RemoveStuff. Assume the CPU is capable of executing two threads concurrently. Give the most specific answer possible. If T1 is trying to execute the statement on line 4, and T2 is trying to execute the statement on line 10 then ________ If T1 is trying to execute the statement on line 5, and T2 is trying to execute the statement on line 10, ________ If T1 is trying to execute the statement on line 5, and T2 is trying to execute the statement on line 12, ________ If T1 is trying to execute the statement on line 4, and T2 is trying to execute the statement on line 9, ________
T2 will proceed and T1 will stall This situation is not possible This situation is not possible Either T1 or T2 will proceed, but not both
Consider the MVC chat system from lecture. If we continued development on it, we would add an ErrorEvent to the controller and invoke it when appropriate. In the View, suppose we have the following code in the button handler for the connect button: { ... controller.Connect(serverAddress.Text);controller.ErrorEvent += ShowError; ... } The View also defines this method: private void ShowError(string err){ DisplayAlert(err); } Assume the controller always correctly fires the event when an error occurs. Which of the following is true about the behavior of this program? Select all that apply. The View will always show an error when it occurs The View will never show an error because the event handler that it registers is private The View will show some of the errors, but not necessarily all of them The View is violating MVC principles The code contains a race condition
The View will show some of the errors, but not necessarily all of them The code contains a race condition
Consider the following snippet of code from a web server to return information to a browser using the PS7 networking library: Networking.Send(state.TheSocket, httpOkHeader + "hello"); state.TheSocket.Close(); Assume before this code runs that the socket is connected and httpOkHeader is a proper HTTP header. Which of the following are possible outcomes? Select all that apply. The browser will "spin", showing that it expects more data indefinitely The browser will display an incomplete web page, but will appear to be done loading The browser will display a complete web page, and will appear to be done loading The browser can not display anything because it is not formatted as html
The browser will display an incomplete web page, but will appear to be done loading The browser will display a complete web page, and will appear to be done loading
Suppose someone runs a single SQL command that deletes certain rows from a table on a MySQL database, and the server machine loses power some time after starting to process the command: possibly before finishing, and possibly after finishing. Which of the following outcomes are possible? Select all that apply. The command does not modify the database in any way The command partially completes, and leaves the database in an inconsistent state The command partially completes, and leaves the database in a consistent state The command fully completes, and leaves the database in an inconsistent state The command fully completes, and leaves the database in a consistent state
The command does not modify the database in any way The command fully completes, and leaves the database in a consistent state
Consider question 3. If message was a Label instead of a string, and we wanted to use that label to convey information to a user, we would change line 4 to this: message.Text = "not enough items"; Suppose the message label belongs to a MAUI Application. If we were to execute the program and TryDequeue ran on its own separate thread (called Thread2), which of the following are true? Select all that apply. There is a potential race condition between the thread running TryDequeue and the MAUI message loop thread Regardless of potential race conditions, we need to use a Dispatcher to modify the label If we use a Dispatcher, the MAUI message loop thread will execute the code that modifies the label If we use a Dispatcher, Thread2 will invoke the code that modifies the label
There is a potential race condition between the thread running TryDequeue and the MAUI message loop thread Regardless of potential race conditions, we need to use a Dispatcher to modify the label If we use a Dispatcher, the MAUI message loop thread will execute the code that modifies the label
Consider three different versions of a method that are supposed produce the same result: int Version1(int x){ int total = 0; for(int i = 0; i < 10; i++)total += Foo(x); return total; } int Version2(int x){ int total = 0; int temp = Foo(x); for(int i = 0; i < 10; i++)total += temp; return total; } int Version3(int x){ int total = 0; int temp = x >> 1; for(int i = 0; i < 10; i++)total += temp; return total; } Here is the definition of Foo, defined somewhere in the same file that the compiler can easily analyze. int Foo(int x){ return x / 2; } Assume a modern optimizing compiler is used. Which of the following are true? Select all that apply. Version2 is likely to be faster than Version1 Version3 is likely to be faster than Version1 Version3 is likely to be faster than Version2 Version3 has a bug and does not produce the correct result There should be no performance difference between the three versions
There should be no performance difference between the three versions
Suppose you write a program that must complete two independent tasks. Each task takes 1 minute when run individually. You write two versions of the program: a single-threaded version that runs one task after the other a multi-threaded version that runs both tasks concurrently, and starts them at the same time You also have two computers: a computer with a single-core CPU a computer with a dual-core CPU Assume that aside from the number of cores, the CPUs and everything else about the computers are identical. Assume the time cost of context switching is negligible. When running the single-threaded version of the program on the single-core computer: The total processing time will be _____ minutes The time between starting one task and finishing it will be _______ minute When running the multi-threaded version of the program on the single-core computer: The total processing time will be ______ minute The time between starting one task and finishing it will be _______ minute When running the multi-threaded version of the program on the dual-core computer: The total processing time will be ________ minute The time between starting one task and finishing it will be _____ minutes
When running the single-threaded version of the program on the single-core computer: The total processing time will be 2 minutes The time between starting one task and finishing it will be 1 minute When running the multi-threaded version of the program on the single-core computer: The total processing time will be 1 minute The time between starting one task and finishing it will be 1 minute When running the multi-threaded version of the program on the dual-core computer: The total processing time will be 1 minute The time between starting one task and finishing it will be 0.5 minutes
Consider the below C# program. static void Worker1(){ Thread t2 = new Thread(Worker2); t2.Start(); Thread.Sleep(1000); Console.Write("b"); } static void Worker2() { Console.Write("c"); } static void Main(string[] args){ Thread t = new Thread(Worker1); Console.Write("a"); t.Start(); } Which of the following are possible outputs of this program? abc acb bac bca cab cba
abc acb
SQL is a _____ database __________ , and MySQL is a _________ database ____________ .
relational query language relational management system
Suppose some program uses three objects as keys for mutex locks (a, b, and c). Thread 1 and Thread 2 execute the following blocks of code concurrently: /* Thread 1 */ lock(a) { lock(b) { lock(c) { } } } /* Thread 2 */ lock(c){ lock(a){ } } This program results in deadlock: always sometimes never
sometimes
