Docsity

MATLAB Coding Questions and Answers, Exams of Computer Science

Toggle sidebar
bg1
1. (QID 1136) 20/20 points
A
B
C
D
E
Given the following code:
arr = {'Intel', [8 9 2], {'Arc Reactor', [100 100 200], 'no'}, 'yes'};
A = arr{3}{1}(2);
arr{1} = [];
B = class(arr{2}(1));
arr2 = {arr arr(1)};
C = length(arr2);
D = arr2{1}{3}{1};
E = 4 + arr(2);
After the code is run in MATLAB what would be the value of the following variables? Enter the values as you would
enter them in MATLAB, strings need to be in single quotes, logicals need to be true or false, and vectors need to
be in square brackets (you do not need to type A= or B= etc.). If the code runs an error type 'error'. Assume the
code continues to run after any errors occur. Do not comment your code.
2. (QID 1152) 20/20 points
There is a file in the current directory called 'yogi.txt'. It contains the following FIVE lines:
Baseball is 90% mental,
and the other half
is physical.
- Yogi Berra
CS1371 Fall 2010 - Test 2
BUTLER, KENNETH AARON - kbutler7
Correct Answer: 'r'
'r'
(4 points)
Correct Answer: 'double'
'double'
(4 points)
Correct Answer: 2
2
(4 points)
Correct Answer: 'Arc Reactor'
'Arc Reactor'
(4 points)
Correct Answer: 'error'
'error'
(4 points)
https://t-square.gatech.edu/access/content/attachment/d41cf706...
1 of 7 4/12/12 8:44 PM
pf3
pf4
pf5

Partial preview of the text

Download MATLAB Coding Questions and Answers and more Exams Computer Science in PDF only on Docsity!

1. (QID 1136) 20/20 points A B C D E Given the following code: arr = {'Intel', [8 9 2], {'Arc Reactor', [100 100 200], 'no'}, 'yes'}; A = arr{3}{1}(2); arr{1} = []; B = class(arr{2}(1)); arr2 = {arr arr(1)}; C = length(arr2); D = arr2{1}{3}{1}; E = 4 + arr(2); After the code is run in MATLAB what would be the value of the following variables? Enter the values as you would enter them in MATLAB, strings need to be in single quotes, logicals need to be true or false, and vectors need to be in square brackets (you do not need to type A= or B= etc.). If the code runs an error type 'error'. Assume the code continues to run after any errors occur. Do not comment your code. 2. (QID 1152) 20/20 points There is a file in the current directory called 'yogi.txt'. It contains the following FIVE lines: Baseball is 90% mental, and the other half is physical.

  • Yogi Berra

CS1371 Fall 2010 - Test 2

BUTLER, KENNETH AARON - kbutler

'r' (4 points)^ Correct^ Answer:^ 'r' 'double' (4 points)^ Correct^ Answer: 'double' 2 (4 points)^ Correct^ Answer:^2 'Arc Reactor' (4 points)^ Correct^ Answer: 'Arc Reactor' 'error' (4 points)^ Correct^ Answer:^ 'error'

A

B

C

D

Is the file closed correctly? Type 'yes' or 'no' (with the single quotes).

3. (QID 1168) 10/10 points Function Name: simplePaintMixer Now consider the following code. Trace the code to determine the values of the variables below. name = 'yogi.txt'; N = fopen(name,'r'); f = [fgetl(N) fgetl(N)]; y = find(f==' '); g = fgetl(N); fgetl(N); h = fgetl(N); j = fgetl(N); A = f(y(3)+1:y(4)-1); B = length(j); C = class(g); D = f(f=='%'|f=='9'); fclose(N); After the code is run in MATLAB what would be the value of the following variables? Enter the values as you would enter them in MATLAB, strings need to be in single quotes, logicals need to be true or false, and vectors need to be in square brackets (you do not need to type A= or B= etc.). If the code runs an error type 'error'. Assume the code continues to run after any errors occur. Do not comment your code. A: B: C: D: Is the file closed correctly? Type 'yes' or 'no' (with the single quotes). 'mental,and' (4 points)^ Correct^ Answer: 'mental,and' 1 (4 points)^ Correct^ Answer:^1 'char' (4 points)^ Correct^ Answer: 'char' '9%' (4 points)^ Correct^ Answer:^ '9%' 'yes' (4 points)^ Correct^ Answer: 'yes'

Graded By: CJ Inputs (3): (cell) a cell array of items in inventory (cell) a cell array of the items' prices (char) an item to search for Output (1): (double or char) the price of the item, or the string 'Item Does Not Exist' if no item is found Function Description: This function takes in two cell arrays of equal length. The first contains strings, which are the names of items in inventory. The second contains doubles, which are the prices of those items. The third input is a string which is the item to search for. If the item exists in inventory, then the output is the items price. If the item does not exist in inventory, then the output is the string 'Item Does Not Exist'. See the test cases below for clarification. When performing comparisons, case is important. There will be no duplicate item names. Test Cases: items = {'Milk', 'Eggs', 'Jackhammer', 'Dump Truck'}; prices = {2.49, 1.89, 45.95, 30000}; A = priceLookup(items, prices, 'Jackhammer'); A => 45. B = priceLookup(items, prices, 'Flux Capacitor'); B => 'Item Does Not Exist' function out = priceLookup(ca1, ca2, item) out = 'Item Does Not Exist; for i = 1:length(ca1) if strcmp(ca1{i},item) out = ca2{i}; end end end TA Comments Good Job!

5. (QID 759) 20/20 points Function Name: findHouses Inputs (2): - (struct) a structure array of house listings - (double) a minimum price

Graded By: TP Outputs (1): - (struct) a structure array of the house listings that meet the search criteria Function Description: The input structure array contains information on house listings with two fields, 'price' and 'zipcode'. The output is a structure array containing only the listings located in Zip Code '08873' AND that are more expensive (greater) than the minimum price given as an input to the function. Test Case: list = struct('price', {85000, 90000, 75000}, 'zipcode', {'08873' '08844' '08873'}); A = findHouses(list, 80000); A -> 1x1 struct (A.price -> 85000 and A.zipcode -> '08873') function out = findHouses (struct, minimum) out.price = 'None'; out.zipcode = 'None'; count = 1; for i = 1:length(struct) if strcmp(struct(i).zipcode,'08873') && struct(i).price > minimum out(count).price = struct(i).price; out(count).zipcode = struct(i).zipcode; count = count + 1; end end end TA Comments Good Job!

6. (QID 1158) 15/15 points Function Name: countNames Inputs (2): (char) the filename of a .txt file containing a list of names (char) a last name to search for Output (1): (double) the number of names in the file that have the specified last name Function Description: This function takes in the name of a text file. The text file contains a list of names (firstname lastname). The second input to the function is a last name to search for. The output of the function is the number of names in the file where the last name matches the second input. Comparisons should be made case-sensitive. If no matches

Section Your Score Possible Score

  • Multiple Choice 0.00 0.
  • Fill In The Blank 40.00 40. - Coding 60.00 60. - Grand Total 100.00 100.