Practice Questions for Introduction to Programming - Solved | COMP 110, Assignments of Computer Science

Material Type: Assignment; Class: Introduction to Programming; Subject: COMPUTER SCIENCE; University: University of North Carolina - Chapel Hill; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 03/10/2009

koofers-user-oea
koofers-user-oea 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
In-Class Exercises
Section 1: Short answer
a. What is a class? An object?
b. What is an array in Java?
c. What is overloading?
d. What is a constructor method?
Section 2
1. Write the definition heading for the following methods. Do not write the body of the
method, just the header. You can name all the methods by “myMethod”. You can name
the input parameters in any legal way.
a) a public non-static method that takes an int value as input and returns a double value
public double myMethod(int n)
b) a private static method that takes two String values as input and returns nothing
private static void myMethod(String s1, String s2)
c) the constructor method of the class Pet which takes two double values as input
public Pet(double n, double m)
d) a public static method that takes a float array as input and returns a double array.
public static double[] myMethod(float[] a)
pf3
pf4

Partial preview of the text

Download Practice Questions for Introduction to Programming - Solved | COMP 110 and more Assignments Computer Science in PDF only on Docsity!

In-Class Exercises

Section 1: Short answer a. What is a class? An object? b. What is an array in Java? c. What is overloading? d. What is a constructor method? Section 2

  1. Write the definition heading for the following methods. Do not write the body of the method, just the header. You can name all the methods by “myMethod”. You can name the input parameters in any legal way. a) a public non-static method that takes an int value as input and returns a double value public double myMethod(int n) b) a private static method that takes two String values as input and returns nothing private static void myMethod(String s1, String s2) c) the constructor method of the class Pet which takes two double values as input public Pet(double n, double m) d) a public static method that takes a float array as input and returns a double array. public static double[] myMethod(float[] a)

Section 3 3.1 What does this method return when first <= last? What does this method return when first >= last? public static int mystery(int first, int last) { if (first > last) return 0; else if (first == last) return first; else return first +last; } when first<=last, if first == last, return first if first < last, return first+last when first>=last, if first ==last, return first if first > last, return 0 3.2. a) Declare and create an array of int type with length 100 and initialize the elements with their respective indices in the array. e.g., the value of the first element is 0, the value of the second element is 1… int[] a = new int[100]; for(int i=0; i<100; i++) a[i] = i; b) Compute the sum of the elements of the array and output the sum. int sum = 0; for( int i =0; i < 100; i++) sum += a[i]; System.out.println(sum);

Output: 3 6 5