Quiz 1

  1. Rank the following in terms of abstraction, with 1 being the lowest / closest to the bare metal of a computer, and 4 being the highest level / greatest amount of abstraction (4pts)

  2. Explanation: Assembly language is pretty much directly inputting the bits that the computer makes use of, so it's the lowest-level option.

    Next up from there are compiled languages, like C++, since they still the entire program is translated (compiled) into machine language before running.

    Next up from there are the interpreted languages, which are constantly shuffling data back and forth between you and the computer. This generally involves a bit more separation from the raw metal of the machine, so they're 3rd in the list.

    Finally, markup languages like HTML, XML, and JSON aren't programming languages at all- they're just structured data. Definitely very important, but the farthest away from any kind of low-level implementation of logic (since they don't actually contain any logic).
  3. True or False: The object (.OBJ) files created by the compiler are meant to be read by humans. (1 pt).

    FALSE. The .OBJ files are fed into the linker to generate the final executable. They contain the results after compiling the code, and are therefore not meant to be read by humans (they're for the computer).
  4. Which of the following lines could be used to store user input into the firstName variable? Circle your answer (2 pts)

    A is the correct choice. We need to use cin to take input (cout is used for output). Once we're using cin, we also need to use the proper operator. The >> operator is what we want, and will extract input from the default input stream. The << operator, on the other hand, will output to the default output stream.
    Incidentally, I mistakenly had the exact same answer for B and D- I meant to have D read as "cout << firstName", which would be the correct way to output the firstName variable. Regardless, A is the correct answer.
  5. The following code is almost correct, but would fail to compile. Add to it so that it would compile and successfully and print “Hello World” to the console. Hint: there are two different changes you could make, either one of which would fix the issue. (3pts)

    #include <iostream>
    
    int main()
    {
    	cout >> "Hello, world\n";
    	return 0;
    }
    
    So the problem here is that we're using the cout function, but it's possible that there is more than one "cout" function defined. So we need to make it clear to the compiler that we intend to use the cout function that is defined in the iostream library. The iostream library is a part of the standard library, so we can fix the problem by ensuring that we indicate that we're in the context of the standard library.

    There are two ways we could do that. The first one is to preifx our call to cout with "std::". The "std" is shorthand for the standard library, and the "::" is the scope resolution operator. So "std::cout" translates into "the cout function defined in the standard library". That would give us something like the following:

    #include <iostream>
    
    int main()
    {
    	std::cout >> "Hello, world\n";
    	return 0;
    }
    

    That would work just fine, but would require that we continue to use "std::" every time we wanted to use the cout (or cin, or many other functions). A better way would be to just tell the compiler that the entire file should reference the standard library. To do that, we can include a line at the top of the file, as in:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	cout >> "Hello, world\n";
    	return 0;
    }
    

    That would be equivalent to having added "std::" to all of our function calls.