Docsity

Solutions for Final Exam - Java Programming | COP 2250, Exams of Javascript programming

Toggle sidebar
bg1
1
NAME: PANTHER ID:
1. (3 points) Write down the following code using “while” loop. Also, compute the output and print
it.
String s = “Tallahassee”; Your code goes here:
int count = 0;
for(int i =0; i <s.length( ); i++)
{
if(s.charAt(i)==’a’)
count ++;
}
System.out.println(count);
OUTPUT
2. (2 points) What is the output of the following code:
int [ ] a = new int[5]; OUTPUT
for(int i = 0; i < a.length; i++)
{
a[i] = i * 3;
System.out.println(a[i]);
}
3. (3 points) Complete the following method that multiplies all the elements of the input array with
2, and returns the modified array.
public int [ ] foo (int [ ] a)
{
for(int i = 0; i < a.length( ); i++)
a[i] = a[i] * 2;
return a;
}
String s = “Tallahassee”;
int count = 0;
int i = 0;
while(i < s.length( ) ){
if(s.charAt(i) == ‘a’)
count ++;
i++;
}
System.out.println(count);
3
0
3
6
9
12
pf3
pf4

Partial preview of the text

Download Solutions for Final Exam - Java Programming | COP 2250 and more Exams Javascript programming in PDF only on Docsity!

NAME: PANTHER ID:

1. (3 points ) Write down the following code using “while” loop. Also, compute the output and print it.

String s = “Tallahassee”; Your code goes here: int count = 0; for(int i =0; i <s.length( ); i++) { if(s.charAt(i)==’a’) count ++; }

System.out.println(count);

OUTPUT

2. (2 points) What is the output of the following code:

int [ ] a = new int[5]; OUTPUT

for(int i = 0; i < a.length; i++) { a[i] = i * 3; System.out.println(a[i]); }

3. (3 points) Complete the following method that multiplies all the elements of the input array with 2, and returns the modified array.

public int [ ] foo (int [ ] a) {

for(int i = 0; i < a.length( ); i++) a[i] = a[i] * 2;

return a;

String s = “Tallahassee”; int count = 0; int i = 0; while(i < s.length( ) ){ if(s.charAt(i) == ‘a’) count ++; i++; } System.out.println(count);

0 3 6 9 12

4. ( 4 points ) Write a program that will prompt the user to enter the full name. Your program should get the fullname and print the last name backwards. (Sample input: “Vito Corleone”; Output: “enoelroC”)

import java.util.Scanner; public class Backwards { public static void main( String [] args){

Scanner scan = new Scanner (System.in); System.out.println(“Please, Enter your full name: “); String fullName = scan.next( ); int index = fullName.indexOf(‘ ‘);

for(int i =fullName.length( ) -1; i > index; i--) System.out.print(fullName.charAt(i));

5. (4 points) Complete the following method definition that takes an integer array as input and returns the number of odd elements in the array.

public int foo ( int [ ] a) {

int count = 0; for(int i =0; i < a.length; i++) { if(a[i] % 2 == 1) count ++; }

return count;

7. (3 points) Assuming that you have already defined DVD class, write down the code that will create two DVDs; DVD1 = {“The Matrix”, 120) and DVD2 = {“Gladiator”, 150}. Also, write down the code to print the information about these two DVDs and check if these two DVDs are equal.

DVD DVD1 = new DVD (“The Matrix”, 120); DVD DVD2 = new DVD (“Gladiator”, 150);

System.out.println(DVD1); System.out.println(DVD2);

if (DVD1.equals(DVD2) System.out.println(“DVD1 and DVD2 are equal”); else System.out.println(“DVD1 and DVD2 are NOT equal”);

8. (6 points) Check the correct answer for each of the following questions.

(i). In a class, if a field is private, It can be accessed directly from any class. It can be accessed directly from inside its class.

(ii). What is the keyword used for declaring a constant? static final constant

(iii). What is a constructor’s return type? void The class name No return type

(iv). In a typical class, what is the general recommendation for access modifiers? Instance variables are private , and methods are private. Instance variables are private , and methods are public. Instance variables are public , and methods are private. Instance variables are public , and methods are public.

(v). How do you retrieve the number of elements in an array a? a.length( ) a.length a.size( )

(vi). All the elements of an array must be of the same type. true false