CSE 1310 - Practice Problems - Practice Set 1


Practice Problem 1

public class question1
{
  public static void main(String[] args)
  {
    int a = 3;
    int b = 6;
    int c = 9;
    int d = 12;
    int e = 15;
    int f = 18;
    int g = 21;
    
    if ((a == 3) && (b == 4))
    {
      d = 20;
    }
    if ((a == 3) || (c == 5))
    {
      e = 30;
    }
    if (!(b == 5))
    {
      f = 40;
    }
    else
    {
      g = 50;
    }

    System.out.printf("d = %d\n", d);
    System.out.printf("e = %d\n", e);
    System.out.printf("f = %d\n", f);
    System.out.printf("g = %d\n", g);
  }
}
What does this program print?


Practice Problem 2

public class question2
{
  public static void main(String[] args)
  {
    int a = 3;
    int b = 6;
    int c = 9;
    int d = 12;
    int e = 15;
    int f = 18;
    int g = 21;
    
    if (e == 15)
    {
      a = 10;
    }
    else if (f == 18)
    {
      b = 20;
    }
    if (!(g == 21))
    {
      c = 30;
    }
    else
    {
      d = 40;
    }

    System.out.printf("a = %d\n", a);
    System.out.printf("b = %d\n", b);
    System.out.printf("c = %d\n", c);
    System.out.printf("d = %d\n", d);
  }
}

What does this program print?


Practice Problem 3

public class question3
{
  public static void main(String[] args)
  {
    int a = 10;
    int b = 20;
    while ((a + b > 15) || (a < 15))
    {
      a += 2;
      b -= 10;
      System.out.printf("a = %d, b = %d\n", a, b);
    }
  }
}
What does this program print?


Practice Problem 4

import java.util.Scanner;

public class question4
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("What is the temperature? ");
    double temperature = in.nextDouble();

    // Your code goes here.    
  }
}
Write the code that is needed to complete the above program, so that it satisfies these specs:


Practice Problem 5

import java.util.Scanner;

public class question5
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter an integer: ");
    int number = in.nextInt();

    // Your code goes here.
  }
}
Write the code that is needed to complete the above program, so that it satisfies these specs:


Practice Problem 6

import java.util.Scanner;

public class question6
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter word1: ");
    String word1 = in.next();
    System.out.printf("Enter word2: ");
    String word2 = in.next();

    // Your code goes here.    
  }
}
Write the code that is needed to complete the above program, so that it counts and prints the number of characters in word2 that also appear in word1. Character comparisons should be case sensitive; for example, 'c' and 'C' should be treated as different characters.

For example, if the user enters "abc" for word 1 and "Cabbage" for word 2, then four letters (a, b, b, a) of word2 appear in word 1, and the program output should look exactly like this:

Enter word1: abc
Enter word2: Cabbage
Counted 4 letters
As another example, if the user enters "Cabbage" for word 1 and "abc" for word 2, then two letters (a, b) of word2 appear in word 1, and the program output should look exactly like this:
Enter word1: Cabbage
Enter word2: abc
Counted 2 letters


Practice Problem 7

import java.util.Scanner;

public class question7
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter a word: ");
    String word = in.next();
    
    // Your code goes here.    
  }
} 
Write the code that is needed to complete the above program, so that: For example, if the word is "Cabbage", then the word contains two b's in a row, and the output should be exactly this:
Enter a word: Cabbage
Found consecutive identical characters.
As another example, if the word is "AaBb", the output should be exactly this:
Enter a word: AaBb
Did not find consecutive identical characters.
Hint: to check if word contains two identical characters, you need to check different positions k in word, to see if the character at position k is equal to the character at position k+1.


Practice Problem 8

import java.util.Scanner;

public class question8
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter a word: ");
    String word = in.next();
    
    // Your code goes here.    
  }
}
For the purposes of this problem, the following letters are called special: G, L, N, R, T, and their lower-case versions.

Write the code that is needed to complete the above program, so that it counts and prints the number of special letters in the word that the user enters.

For example, if the user enters "Arlington", the program output should be this:

Enter a word: Arlington
Found 6 special letters.
As another example, if the user enters "Java", the program output should be this:
Enter a word: Java
Found 0 special letters.


Practice Problem 9

import java.util.Scanner;

public class question9
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter an integer: ");
    int number = in.nextInt();
    
    // Your code goes here.    
  }
}
Write the code that is needed to complete the above program, so that: For example, if the number is 2, the program output should be exactly this:
Enter an integer: -2
negative
As another example, if the number is 5, the program output should be exactly this:
Enter an integer: 5
6
9
As another example, if the number is 6, the program output should be exactly this:
Enter an integer: 6
6
9
12


Practice Problem 10

import java.util.Scanner;

public class question10
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter some text: ");
    String text = in.nextLine();
    
    // Your code goes here.    
  }
}
Write the code that is needed to complete the above program, so that it prints the text entered by the user in multiple lines, so that: For example, if the user enters text "hello world", the program output should be exactly this:
Enter some text: hello world
hello world
hello worl
hello wor
hello wo
hello w
hello 
hello
hell
hel
he
h