Computer Science : Program Implementation

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #1 : Programming Constructs

Which of the following code snippets declares an array of integers using an array literal?

Possible Answers:

int a = {42,26,134,-13,45,234};

int[] a = {42,26,134,-13,45,234};

int[] a = new int[6];

a[0] = 42;

a[1] = 26;

a[2] = 134;

a[3] = -13;

a[4] = 45;

a[5] = 234;

int[] a;

a[0] = 42;

a[1] = 26;

a[2] = 134;

a[3] = -13;

a[4] = 45;

a[5] = 234;

int[] a = {0:42,1:26,2:134,3:-13,4:45,5:234};

Correct answer:

int[] a = {42,26,134,-13,45,234};

Explanation:

There are two things to pay attention to for this question.  First, you can make a simple array literal in Java by enclosing the list of array elements in a pair of curly braces.  Thus, the array literal

{42,26,134,-13,45,234}

is just fine!  However, you do not give the indices.  These are inferred.  (The first element is at 0, the second at 1, etc.) 

Now, you just have to assign this to an array object.  Thus the following is wrong:

int a = {42,26,134,-13,45,234};

You need to have the [] to indicate that a is an array:

int[] a = {42,26,134,-13,45,234};

Example Question #2 : Program Implementation

What is the difference between double and int?

Possible Answers:

Int can hold real numbers and double can only hold imaginary numbers.

There is no difference.

Int can hold positive numbers only and double can hold all numbers.

Int can only hold integer values, double can hold any number including decimals.

Int can hold all values whereas double can only store two-digit values.

Correct answer:

Int can only hold integer values, double can hold any number including decimals.

Explanation:

Int can only store integer values, whereas double can hold both integers and decimals, but at the same time uses more memory.

Example Question #1 : Variable Declarations

int x=2;

double y=2.1;

float z=3.0;

int c=(x*y) + z;

What is the value of c

Possible Answers:

7

6

6.3

7.1

7.2

Correct answer:

7

Explanation:

Remember that if a variable is declared as an integer, it can't have any decimals.

int c=(x*y) + z;

int c=(2*2.1)+3.0

From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.

int c=4.2+3

int c= 4+3

int c=7

Any leftover fractions are cut off, so the answer is shortened to just 7.

Example Question #1 : Program Implementation

#include <iostream>

using namespace std;

int main()
{
string str("ComputerScience");
string mystr;
str = str + "SampleQuestionCS";
mystr = str.substr(7,8);

return 0;
}

What is the value of mystr after this code is run?

Possible Answers:

Science

rScience

QuestionCS

ComputerS

SampleQuestion

Correct answer:

rScience

Explanation:

Two strings are created here: str and mystr. 

In the 3rd line of main, str is concatenated with another string, making it even longer.

In the fourth line, the substr function is called. This function takes in 2 inputs, the starting index and the length of the string.

So, starting from index 7 of str, we get "r". Now, grabbing a total of 8 characters, including the "r", we get "rScience".

mystr="rScience".

Example Question #11 : Program Implementation

  • Declare a string set to the the word cat. 
  • Declare a list of strings with the words hello, mama, and meow in it.
  • Declare a hashmap of <string, string> <key, value> pairs with the word world having a value of earth.
Possible Answers:

String cat = "cat";

 

List<String> stringList = new ArrayList<String>();

 

HashMap<String, String> hashStrings = new HashMap<String, String>();

hashStrings.put("world", "earth");

String cat;

List<String> stringList;

HashMap<String, String> hashStrings;

String cat = "cat";

 

List<String> stringList = new ArrayList<String>();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

 

HashMap<String, String> hashStrings = new HashMap<String, String>();

hashStrings.put("world", "earth");

 

List<String> stringList = new ArrayList<String>();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

 

HashMap<String, String> hashStrings = new HashMap<String, String>();

hashStrings.put("world", "earth");

Correct answer:

String cat = "cat";

 

List<String> stringList = new ArrayList<String>();

stringList.add("hello");

stringList.add("mama");

stringList.add("meow");

 

HashMap<String, String> hashStrings = new HashMap<String, String>();

hashStrings.put("world", "earth");

Explanation:

The correct answer defines all of the variables specified in the prompt. The answer with just the variable definitons is incorrect because it does not specify the declarations in the prompt. One the answers does not have the first variable defined so it is incorrect. And the final answer does not have the List variable declared, so it is also incorrect. 

Example Question #1 : Variable Declarations

What are the 3 different manners (in terms of accessibilitiy) in which you can declare a variable and what are the differences between the 3?

Possible Answers:

Public: accessible to everyone
Private: accessible to only members of the method
Protected: accessible to subclass

Public: accessible to that sole class

Private: accessible to the method or instance in which it was called

Protected: acts as the default setting 

Public: accessible to everyone
Private: accessible to members of the class
Protected:accessible to classes in the same package and subclasses

Correct answer:

Public: accessible to everyone
Private: accessible to members of the class
Protected:accessible to classes in the same package and subclasses

Explanation:

Public is accessible to everyone regardless if the two classes are in the same package or not. 

Protected: Only allows access of those extended are packaging.

Private: Its meant to keep private in order for only members of that specific class to see it. 

Example Question #1 : Variable Declarations

Which of these produce a typecasting error?

1) float f = double d;

2) double d = int i;

3) int i = byte b;

4) long l = short s;

Possible Answers:

float f = double d;

 

int i = byte b;

double d = int i;

long l = short s;

None of the answers are wrong. 

Correct answer:

float f = double d;

 

Explanation:

These problems all involve implicit type casting. For there to be an issue, you'd have to be converting a primitive that takes up more issue into one that requires less memory. The only one that fits that prerequisite is converting a double into a float.  The primitive data chain is:

byte < short < int < long < float < double

Example Question #1 : Interface Declarations

Which of the following represents an acceptable definition of an interface?

Possible Answers:

public class Rectangle {

     private double length,width;

     public Rectangle(double l,double w) {

          length = l;

          width = w;

     }

     public double getArea() {

          return length * width;

     };

     public double getPerimeter() {

          return 2 * length + 2 * width;

     }

    

}

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

public interface Person {

    private String fName;

    private String lName;

    public String toString();

}

public interface Rectangle {

     private double length,width;

     public Rectangle(double l,double w) {

          length = l;

          width = w;

     }

     public double getArea();

     public double getPerimeter();    

}

public interface Person {

    private String fName;

    private String lName;

    public String toString() {

         return fName + " " + lName;

   }

}

Correct answer:

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

Explanation:

An interface cannot have any member data or implementations for its methods. It only defines a set of methods that are required by anything that implements that interface. Therefore, the only option that is valid is the interface defined as Shape2D.

Example Question #11 : Program Implementation

Which of the following represents a valid declaration of an interface and a class that implements that interface?

Possible Answers:

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

 

public class Circle implements Shape2D {

     private double radius;

     public Circle(double r) {

          radius = r;

     }

     public double getArea() {

          return Math.PI * radius * radius;

     }

     public double getPerimeter() {

          return Math.PI * 2 * radius;

     }

}

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

 

public class Circle implements Shape2D {

     private double radius;

     public Circle(double r) {

          radius = r;

     }

     public double getArea() {

          return Math.PI * radius * radius;

     }

     public double getCircumference() {

          return Math.PI * 2 * radius;

     }

}

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

 

public class Circle implements Shape2D {

     private int radius;

     public Circle(int r) {

          radius = r;

     }

     public int getArea() {

          return Math.PI * radius * radius;

     }

     public int getPerimeter() {

          return Math.PI * 2 * radius;

     }

}

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

 

public class Circle extends Shape2D {

     private double radius;

     public Circle(double r) {

          radius = r;

     }

     public double getArea() {

          return Math.PI * radius * radius;

     }

     public double getPerimeter() {

          return Math.PI * 2 * radius;

     }

}

public interface Shape2D {

     public int getArea();

     public int getPerimeter();

}

 

public class Circle implements Shape2D {

     private double radius;

     public Circle(double r) {

          radius = r;

     }

     public double getArea() {

          return Math.PI * radius * radius;

     }

     public double getPerimeter() {

          return Math.PI * 2 * radius;

     }

}

Correct answer:

public interface Shape2D {

     public double getArea();

     public double getPerimeter();

}

 

public class Circle implements Shape2D {

     private double radius;

     public Circle(double r) {

          radius = r;

     }

     public double getArea() {

          return Math.PI * radius * radius;

     }

     public double getPerimeter() {

          return Math.PI * 2 * radius;

     }

}

Explanation:

In order to implement an interface, you must use the syntax "implements <interface name>". Therefore, for this question, you must choose an option that has implements Shape2D. Also, you must make sure that all of your methods are defined in your class just as they are defined in the interface declaration. You will notice that a variety of the incorrect answers do not do this but instead have wrong types or names in various places.

Example Question #11 : Programming Constructs

Consider the following code:

public static interface Shape2D {

     public double getArea();

}

 

public static class Ellipse implements Shape2D {

     private double r1,r2;

     public Ellipse(double r1,double r2) {

          this.r1 = r1;

          this.r2 = r2;

     }

     public double getArea() {

          return Math.PI * r1 * r2;

     }

}

public static class Circle extends Ellipse {

     private double radius;

     public Circle(double r) {

          super(r,r);

     }

     public double getArea() {

          return super.getArea();

     }

     public double getCircumference() {

          return Math.PI * 2 * radius;

     }

}

 

public static void main(String[] args) {

     Shape2D[] vals = {new Circle(5),new Ellipse(8,5)};

     for(int i = 0; i < vals.length; i++) {

          System.out.println("Area " + (i+1) + ": " + vals[i].getArea());

     }

}

What is the output for this code?

Possible Answers:

Area 0: 78.53981633974483

Area 1: 201.061929829746752

Area 1: 78.53981633974483

Area 2: 125.66370614359172

The code will not compile.

Area 1: 31.41592653589793

Area 2: 201.061929829746752

Area 1: 78.53981633974483

Area 2: 78.53981633974483

Correct answer:

Area 1: 78.53981633974483

Area 2: 125.66370614359172

Explanation:

We are dealing here both with the implementation of an interface as well as inheritance. Notice that the Circle class has a getArea method that uses the superclass's getArea method.  This equation is , but for the Circle , so the equation is the same as the standard . This loop will iterate through the objects, using the appropriate method in each case. Thus, you will get two values: 

, which is relatively close to  or .

, which is relatively close to  or .

Those are close enough to help you estimate for the output.

(Notice that the line numbers need to be "1" and "2".)

Learning Tools by Varsity Tutors