Tina Comston
Comp-111 Exam 1
Franklin University
Exam 1 - Solutions to Practice Questions

Exam Practice Questions  
  • Chapter 1
    •  Describe the various operations performed by the CPU.
      • locates and executes the program instructions
      • carries out arithmethic operations such as addition, subtraction, multiplication, and division
      • fetches data from storage and input/output devices and sends data back
    •  Describe the different between primary and secondary storage.  Be able to provide an example of each.
      • primary storage is fast but expensive and loses all data when power it turned off, typically memory chips (RAM)
      • seconds storage provides less expensive stores that persists without electricity, typically a hard disk
  •  What is an algorithm?
    • A sequence of steps that is unambiguous, executable, and terminating.  The specification of the way to solve a problem.
  •  What is psuedo code?
    • Writing out in English or a combination of English and code what you eventually plan to code.  It's a tool that is used to work out the steps needed to be taken to solve a problem.
    • Describe both syntax and logic errors.
      • A syntax error is a violation of the programming language that is identified by the compiler.
      • A logic error is a when a program compiles clean, but does not do what was expected.  These types of error can only be identified through execution and testing of the program.
    • What are the operations performed by the central processing unit (CPU)?
      • Arithmetic operations
      • Locating and executing program instructions
      • Fetchin data from storage
    • What are the two most important benefits of the Java language?
      • safety and portability
    • What is the file extension for a java source file?  What is the file extension for a compiled java file? How do you compile/call a java executable from the command line?
      • .java is the file extension for a java source file
      • .class is the file extension for a compiled java file
      • To compile from the command line:
        • javac ClassName.java
      • To execute from the command line:
        • java className
    • What will be the output of the following statements:
      • String firstWord = "Hello!";
      • String secondWord = "Study";
      • String thirdWord = "hard.";
      • String newSentence = firstWord + secondWord + thirdWord;
      • System.out.println(newSentence);
      • The output will be:
        • Hello!Studyhard.
        • This, of course, has a logic error in that spaces between the words are missing.
    • Evaluate the following code:
      • String firstWord = "Hello!"
      • secondWord = "Study";
      • String thirdWord = "hard.";
      • String newSentence = firstWord & secondWord + thirdWord;
      • System.out.println(newSentence);
  • Identify the syntax errors.  (There are 3.)
  • Identify the logic errors. (There are 2.)
  • syntax errors
    • The declaration of secondWord does not include the data type of String.
    • The first statement is missing a semi-colon.
    • The & between firstWord and secondWord in the 4th line should be a +.
  • logic errors
    • Missing a space between both the firstWord and secondWord, and between the secondWord and thirdWord variables.
    • What is the difference between the System.out.println() and System.out.print() methods?
      • System.out.println() - this method call will print out the enclosed data and will then advance to a new line
      • System.out.print() - this method call will print out the enclosed data, it will NOT advance to a new line
  • What is the purpose of a compiler?  a processor?
    • Compiler - translates programs written in high level language into machine code.
    • Processor - executes machine instructions
  • Chapter 2
    • Describe the purpose of a constructor.  What is the difference between a default constructor and an explicit constructor?  What return type is specified for a constructor?
      • A constructor builds an object.  It is method that initializes a newly instantiated object.
        • Default constructor - this constructor accepts no explicit parameters.  It typically initializes the new object to default values.
        • Explicit constructor - this constructor accepts explicit parameters.  It typically initializes the new object to the value of the parameters.
      • A constructor does not have a return type.
        • Basic methods will have a return type of either void or the data type that should be returned.  Neither is specified for a constructor as a constructor does not return anything.  It is used to create an object.
    • Given the following definition statements for constructors - create a new object instance.
      • public Dog()
        • Dog tmpDog = new Dog();
          • tmpDog is the object variable name I chose - you could chose any name that follows java syntax and conventions
      • public Car(String inMake, String inColor, int inMPG)
        • Car tmpCar = new Car("Ford", "Blue", 33);
          • tmpCar is the object variable name I chose - any name that follows java syntax and convention is valid
          • "Ford" is the String value I chose for the make, you could use any String value
          • "Blue" is the String value I chose for the color, you could use any String value
          • 33 is the integer value I chose for the MPG, you could use any integer value
      • public House(double inLatititude, double inLongitude, String inColor)
        • House tmpHouse = new House(33.5, 86.0, "Gray");
          • tmpHouse is the object variable name I chose - any name would be find
          • 33.5 is the double value I chose for the latitude, you could use any double value (must have a decimal)
          • 86.0 is the double value I chose for the longitude, you could use any double value
          • "Gray" is the color I chose, you could use any String value
  • Consider the following java code:
    • Car newCar1 = new Car("Chevy", "Blue", 32);
    • Car newCar2 = new Car(newCar1);
    • How many objects are created?
      • 2 objects.  newCar1 and newCar2 are 2 completely separate objects that just happen to have the same values.   Kind of like identical twins.  They look the same, but are actually 2 different people.  
    • Are the instance variables referred to by newCar1 the same as the instance variables referred to by newCar2?
      • No.  Each object has it's own instance variables.
  • Consider the following java code:
    • Car newCar1 = New Car("Chevy", "Blue", 32);
    • Car newCar2 = newCar1;
    • How many objects are created?
      • 1 object.  Both newCar1 and newCar2 point to the same object in memory.  Kind of like someone with a nickname.  Michael can also be referred to by Mike.  Two different names, but the same person.
    • Are the instance variables referred to by newCar1 the same as the instance variables referred to by newCar2?
      • Yes.  Because there is just one object there is only one set of instance variables.
  • Evaluate the following statement:
    • myAccount.deposit(payCheck);
      • Identify the object(s), method(s), parameter(s)
      • How many total parameters (implicit and explicit) are there?
      • Is there an implicit parameter? If yes, what is it?
      • Is there an explicit parameter(s)?  If yes, what is it?
    • Objects are:
      • myAccount - this is a object that is calling the method
    • Methods are:
      • deposit - methods are called by an object and are separated from the object by a period
    • Parameters are:
      • payCheck & myAccount
        • payCheck is an explicit parameter
        • myAccount is an implicit parameter
        • A total of 2 parameters.
    • What makes a valid Java identifier?
      • letters, digits, and underscore or dollar sign
      • cannot start with digit
      • cannot use other symbols
      • no spaces
      • no reserved words
      • variables/methods begin with lower case
      • classes begin with upper case
    • What are the primitive data types in Java?
      • int, double, float, boolean, char
    • Which variable type would be used for the following data?
      • GPA
      • name
      • age (in years)
      • batting average
      • football score
      • customer address
        • GPA - this is usually represented as 4.0 or 3.8, it has decimal places so would be type double.
        • Name - string
        • Age - in years, which is a whole number, type integer
        • Batting average - whenever you see the word average, know that decimal places will possible be needed, type double.
        • football score - always a whole number, type integer
        • customer address - string
          • When deciding between string or integer, ask yourself if calculations will be performed.  For example a zip code contains all numbers, but will calculations be performs against the zip code?  No, so it is best represented by a string, not an integer.
  • Evaluate the following statement:
    • public void deposit( double inAmount)
      • Is this an accessor or mutator method?  How can you tell?
      • Does this method return a value?  If yes, what type is the return value?
      • Does this method accept explicit input parameters?  If yes, what type(s) is the parameter(s)?
    • There is not way to know if this is an access or mutator method without seeing the actual code for the method.  Most likely is it a mutator method because nothing is returned (indicated by the void)
      • The method does not return a value - void
      • It does accept an explicit paramter.  This parameter is type double.
      • The implicit parameter will be the object that calls the method - not specified at this time.
    • What will be the output of the following:
      • System.out.print("COMP");
      • System.out.println("111");
      • System.out.print(" is a ");
      • System.out.println("great class.");
        • COMP111
        • is a great class
          • The println() method will first print the data and then advance to a new line.  the print() method does not advance to a new line.
  • Reviewing each of the following variable names - do any of them violate Java syntax?  violate Java convention?
    • tmpStation - does not violate either
    • return - reserved work, violates java syntax
    • noMore? - punctuation is not permitted, violates java syntax
    • TmpStation - variables should start with lower case, violates java convention
  • Chapter 3
    • Describe abstraction, encapsulation, information hiding, and black box.
      • Abstraction - process of finding the essential feature set for a building block of a program such as a class.
      • Encapsulation - hiding of implementation details
      • Information hiding - protecting data and requiring acces to it through the public interfaces.
      • Black box - testing a method without knowing its implementation
    • What is a unit test?
      • A test that is a program and whose job is to call other methods to ensure the output matches the predicted output.
    • What are javadocs?  How is a javadoc created?  What parameters can be specified for a javadoc? 
      • Javadocs are special comments inserted into a class that are used to create Java API documentation. 
      • Javadocs are created by inserting the comments into a class and then generating the docs.
      • The parameters in a javadoc are:
        • An initial sentence describing the method/instance variable.
        • @param followed by the parameter name and description
        • @return followed by the data type and description
    • What is a local variable?  What is the lifetime of a local variable?
      • A local variable is  a variable that is defined within a method, if statement, loop (inside { } within a method that exists to temporarily store values.
      • A local variable ceases to exist when the block within which it is declared has completed execution.
      • For example, if a local variable is defined within a method, it ceases to exist when the method has completed execution.
      • If a local variable is definied within an IF statement, it ceases to exist when the IF statement has completed execution.
      • If a local variable is defined within a loop, it ceases to exist when the loop has completed execution.
  • How is an implicit parameter denoted within a class?
    • It is denoted by the 'this' keyword.  For example, this.timeInSecs or this.rat.
  • How many parameters, both implicit and explicit are in the following statements, assuming String tmpString = "Hello Class!; ?
    • String newString = tmpString.substr(1, 3);
      • 3 total parameters
        • 1 implicit parameter, the object tmpString
        • 2 explicit parameters passed to the substr() method, 1 & 3
    • String upperString = tmpString.upperCase();
      • 1 total parameters
        • 1 implicit parameter, the object tmpString
    • boolean isEqual = tmpString.equals("Hello Class!");
      • 2 total parameters
        • 1 implicit parameter, the object tmpString
        • 1 explicit parameter, the string "Hello Class!"
  • What are the basic steps for testing a method?   How do you write an assert statement to test a method that returns an integer value?  a double value?  a string value?
    • create the object
    • call the method to be tested
    • check the output to make sure it is what is expected
      • An assert statemen to test:
        • integer - assertEquals(integer value, get method)
        • double - assertEquals(double value, get method, delta value)
        • string - assertEquals(string value, getmethod)
  • Chapter 4
  • Evaluate the following statement, assuming that initially a = 10 and b = 11.
    • c = a + b++;
    • What will a be equal to after this statement is executed?
    • What will b be equal to after this statement is executed?
    • What will c be equal to after this statement is executed?
    • With increments/decrements, you have to look at where the increment/decrement is located.  If it is located in front of the variable, then the increment/decrement occurs first - before the calculation.  If it is located behind the variable, then the increment/decrement occurs last - after the calculation.  In the above example, the increment is located behind the variable.
    • So, first the calculation a + b, 10 + 11, c = 21
    • a does not change, a = 10
    • b is incremented after the calculation, 11 + 1, b = 12
    • Describe final and static with regards to defining variables.
      • final - a value that cannot be changed after it has been initialized.  Used with constant values.
      • static - this type of variable belongs to the class, not to the object instantiated (created) from a class.
    • Define a constant variable with a name of MINS_PER_HR set equal to the value 60.
      • final int MINS_PER_HR = 60;
    • What is an instance variable?  Should an object be manipulated by directly accessing its instance variables? What is the lifetime of an instance variable?
      • An instance variable is a variable that is defined for an object.  It comes into existance when the object is created (or instantiated) and ceases to exist when the object ceases to exist.
      • The principle of encapsulation states that an object should not be manipulated by directly accessing its instance variables.  Instead methods of the object should be called for manipulation.  This would include get and set methods.
      • An instance variable exists as long as the object exists.  When the object ceases to exist the instance variable dies.
        • What will be the result of the following equation?
          • 3 + (4 % 2) - 6 / 4
            • 3 + (0) - 1
            • 2
              • % is the symbol for modulus, this symbol says to divide and return the remainder.  In this case 4 divided by 2 will give 2 remainder 0, so 0 is returned.
              • 6/4 - this is integer division.  Both 6 and 4 are whole number without a decimal.  In integer division the decimal portion is ignored.  6 / 4 would be 1.5.  The decimal portion is ignored giving just 1.
                • Had this been specified as 6.0/4 or 6/4.0 it would change from integer division to double division and the decimal would be kept giving 1.5 as the result.
        • Assuming that you need to generate a class and objects to represent the following - what class would be needed and how many objects should be created of each.
          • orange - 15 oranges
          • banana - 10 bananas
          • papaya - 5 papayas
          • 3 classes would be created
            • orange - 15 object instances created
            • banana - 10 object instances created
            • papaya - 5 object instances created
        • Evaluate each of the following statements and determine the number type.  Will the statement successfully compile?
          • int tmpNum = 40;
            • whole number (integer) - yes will compile
          • double tmpNum = 40.0;
            • contains decimal places (double) - yes will compile
          • int tmpNum = 40.0;
            • whole number (integer) - NO will not compile
          • double tmpNum = 40;
            • double - yes will compile

        • Consider the following equation:
          • double battingAverage = 2.81;
          • int avgRounded = battingAverage;
          • What value will avgRounded contain?  Or will an error result?
            • A compile error will occur.  avgRounded in a variable of data type integer which can only contain whole numbers.  battingAverage is a variable of type double.  When an integer variable is set equal to a double value a compile error results stating that there is a possible loss of precision.