Wednesday, November 28, 2012

Java Pyramid -2


1. create an interactive program that produces the following pyramid.
                                     1
                  212
                 32123
                4321234
               543212345

 Java code: 


import java.util.Scanner;

class Project_4_Bonus {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String choice = "Y";

        do {
            cal_Pyramid();
            System.out.print("continue ? >>> (Y/N): ");
            choice = sc.next();
            System.out.println("\n");
        } while (choice.equalsIgnoreCase("Y"));
    } //end

    public static void cal_Pyramid() {
        System.out.print("Enter the highest number of pyramid : ");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();

        for (int i = 1; i <= x; i++) {

            for (int j = 1; j <= x - i; j++) {
                System.out.print("  ");
            }

            for (int k = i; k >= 1; k--) {
                if (k >= 10) {
                    System.out.print(k);
                } else {
                    System.out.print(" " + k);
                }
            }

            for (int k = 2; k <= i; k++) {
                System.out.print((k >= 10) ? k : " " + k);
            }
            System.out.println();
        }
    }
}



Java- Pyramid Patterns -1


Java Pyramid Patterns by using  methods:


import java.util.Scanner;
 public class Classmate
 {
static int choice;
public static void main(String args[])
{
Scanner ms=new Scanner(System.in);
System.out.print("Enter a number:");
choice=ms.nextInt();
patternA();
patternB();
patternC();
patternD();

}
public static void patternA()
{
System.out.println("Pattern A");
for(int x=1; x<=choice; x++)
{
for(int y=1; y<=x; y++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void patternD()
{
System.out.println("Pattern D");
for(int x=0; x<choice; x++)
{
for(int y=-1; y<x; y++)
System.out.print(" ");
for(int y=x; y<choice; y++)
System.out.print("*");
    System.out.println();
}
}
public static void patternC()
{
System.out.println("Pattern C");
for(int x=choice; x>=1; x--)
{
for(int y=1; y<=x; y++)
{
System.out.print("*");
}
System.out.println();
}
}
public static void patternB()
{
System.out.println("Pattern B");
for(int x=choice; x>=1; x--)
{
for(int y=1; y<x; y++)
System.out.print(" ");
for(int y=x; y<=choice; y++)
System.out.print("*");
    System.out.println();
}
         }
 }

  





Result:


Enter a number: 4

PatternA:
*
**
***
****

PatternB:
   *
  **
 ***
****

PatternC:
****
***
**
*

PatternD:
****
 ***
  **
   *

Continue? (y/n):
----------------------------------------------




Java Sample questions with answers -1



2 points   
Question 31
1.         
Although it is not a requirement of any programming language, it frequently makes sense to use ____ as all or part of a variable’s name if the variable that holds a status.
Answer
the verb to be
a verb
the word status
the abbreviation stat
2 points   
Question 32
1.         
Boolean expressions are named after ____.
Answer
Harold Boolean
Henry Boole
George Boole
Gerhardt Boolean
2 points   
Question 33
1.         
Which of the following is a Java inline comment?
Answer
//
#
$
REM
2 points   
Question 34
1.         
When you need to ask multiple questions before an outcome is determined, you must create a(n) ____ condition.
Answer
dual-alternative
nested
single-alternative
compound
2 points   
Question 35
1.         
A series of nested if statements can also be called a(n) ____ statement.
Answer
n-alternative if
cascading if
case
loop
2 points   
Question 36
1.         
____ are diagrams used in mathematics and logic to help describe the truth of an entire expression based on the truth of its parts.
Answer
Decision matrices
Decision diagrams
Truth diagrams
Truth tables
2 points   
Question 37
1.         
For maximum efficiency, a good rule of thumb in an OR decision is to ____.
Answer
first ask the question that is more likely to be true
first ask the question that is more likely to be false
rewrite as an and decision and ask the question more likely to be true
rewrite as an and decision and ask the question more likely to be false
2 points   
Question 38
1.         
The type of nested if-then-else structure that could be replaced with a case structure is ____.
Answer
inner nested if within the if portion of the outer if
inner if within the else portion of the outer if
one that uses a logical AND
one that uses a logical OR
2 points   
Question 39
1.         
When creating a decision table, you must determine how many possible Boolean value combinations exist for the conditions. If there are two conditions, ____ combinations will exist.
Answer
2
4
8
16
2 points   
Question 40
1.         
The first step in a loop is typically ____.
Answer
compare the variable to a value that determines if the loop stops or continues
initialize the variable
increment the variable
perform the loop action
2 points   
Question 41
1.         
The last step in a loop is usually ____.
Answer
compare the variable to a value that determines if the loop stops or continues
initialize the variable
increment the variable
perform the loop action
2 points   
Question 42
1.         
The piece of pseudocode that represents incrementing the loop control variable is ____.
Answer
rep = 1
rep = rep + 1
while (rep < 5)
print “warning”
2 points   
Question 43
1.         
When a value is incremented ____.
Answer
1 is added to the value
1 is subtracted from the value
the value is multiplied by 1
the value is divided by 1
2 points   
Question 44
1.         
A loop within another loop is known as a(n) ____ loop.
Answer
indefinite
infinite
nested
hidden
2 points   
Question 45
1.         
When one loop appears inside another, the loop that contains the other loop is called the ____ loop.
Answer
indefinite
definite
inner
outer
2 points   
Question 46
1.         
When a loop control variable is not altered during loop execution, a(n) ____ loop may result.
Answer
enlarged
infinite
broken
default
2 points   
Question 47
1.         
The ____ loop provides three actions in one compact statement.
Answer
for
while
do until
repeat
2 points   
Question 48
1.         
In a for loop, a(n) ____ value is used to control how the loop control variable is incremented.
Answer
step
group
id
sentinel
2 points   
Question 49
1.         
Sometimes a ____ loop is called a repeat until loop.
Answer
for
while
do until
repeat
2 points   
Question 50
1.         
The statement that is true of a structured loop is that ____.
Answer
the loop can exit from any point
the loop body must execute at least once
the loop condition represents the only exit from the loop
the loop can repeat an infinite number of times
2 points   
  Answers:

·         Question 1
2 out of 2 points

When data items are stored for use on computer systems, they are often stored in what is known as a data hierarchy.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif True
·         Question 2
2 out of 2 points

Whenever a main program calls a module, the logic transfers to the main program.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif False
·         Question 3
2 out of 2 points

When you must satisfy two or more criteria to initiate an event in a program, you must make sure that the second decision is made entirely independently of the first decision.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif False
·         Question 4
2 out of 2 points

A decision table consists of four parts.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif True
·         Question 5
2 out of 2 points

____ data items may involve organizing them, checking them for accuracy, or performing mathematical operations on them.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
Processing
·         Question 6
2 out of 2 points

____ errors are detected by a compiler.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
Syntax
·         Question 7
2 out of 2 points

After a programmer plans the logic of a program, the next step is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
coding the program
·         Question 8
2 out of 2 points

Typically, a programmer develops a program’s logic, writes the code, and then ____ the program, which may generate a list of syntax errors.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
compiles
·         Question 9
2 out of 2 points

Using ____ involves writing down all the steps you will use in a program.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
pseudocode
·         Question 10
2 out of 2 points

A variable name is also called a(n) ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
identifier
·         Question 11
2 out of 2 points

The best choice for a variable name representing an interest rate is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
interest
·         Question 12
2 out of 2 points

A program with a(n) ____ never ends.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
infinite loop
·         Question 13
2 out of 2 points

If a flowchart has six processing steps and a page provides room for only three, you can use a ____ to create the completed flowchart.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
connector
·         Question 14
2 out of 2 points

The problem with the following statement is that ____.
100 = grade
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
the value on the left must be a variable name
·         Question 15
2 out of 2 points

The following pseudocode is an example of a(n) ____ structure:
  get number
  while number is positive
    add to sum
    get number
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
loop
·         Question 16
2 out of 2 points

Another name for a loop structure is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
iteration
·         Question 17
2 out of 2 points

The maximum number of entry points that any programming structure can have is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
one
·         Question 18
2 out of 2 points

Suppose a program will read 100 data records, and you read the first data record in a statement that is separate from the other 99. This is called a ____ read.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
priming
·         Question 19
2 out of 2 points

One way to straighten out a flowchart segment that is not structured is to use what is called the “____” method.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
spaghetti bowl
·         Question 20
2 out of 2 points

case structure can be replaced by one or more ____ structures.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
if-then-else
·         Question 21
2 out of 2 points

____ documentation includes all the supporting paperwork that programmers develop before they write a program.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
External program
·         Question 22
2 out of 2 points

The ____ of documentation is typically written first.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
output
·         Question 23
2 out of 2 points

If you are producing a report from stored data, you frequently will be provided with a(n) ____ that describes the data contained in a file.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
file description
·         Question 24
2 out of 2 points

____ is the process of paying attention to important properties while ignoring nonessential details.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
Abstraction
·         Question 25
2 out of 2 points

Newer ____ programming languages allow you to use English-like vocabulary in which one broad statement corresponds to dozens of machine instructions.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
high-level
·         Question 26
2 out of 2 points

If a subroutine or function is useful and well-written, you may want to use it more than once within a program or in other programs. This is known as ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
reusability
·         Question 27
2 out of 2 points

In a flowchart, you draw each module separately with its own sentinel symbols. The symbol that is the equivalent of the start symbol in a program contains the ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
name of the module
·         Question 28
2 out of 2 points

The ____ statement is used to indicate the end of a module.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
return
·         Question 29
2 out of 2 points

The computer keeps track of the correct memory address to which it should return after executing a module by recording the memory address in a location known as the ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
stack
·         Question 30
2 out of 2 points

A variable that is used known to the entire program is a ____ variable.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
global
·         Question 31
2 out of 2 points

Although it is not a requirement of any programming language, it frequently makes sense to use ____ as all or part of a variable’s name if the variable that holds a status.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
the verb to be
·         Question 32
2 out of 2 points

Boolean expressions are named after ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
George Boole
·         Question 33
2 out of 2 points

Which of the following is a Java inline comment?
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
//
·         Question 34
2 out of 2 points

When you need to ask multiple questions before an outcome is determined, you must create a(n) ____ condition.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
compound
·         Question 35
2 out of 2 points

A series of nested if statements can also be called a(n) ____ statement.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
cascading if
·         Question 36
2 out of 2 points

____ are diagrams used in mathematics and logic to help describe the truth of an entire expression based on the truth of its parts.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
Truth tables
·         Question 37
2 out of 2 points

For maximum efficiency, a good rule of thumb in an OR decision is to ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
first ask the question that is more likely to be true
·         Question 38
2 out of 2 points

The type of nested if-then-else structure that could be replaced with a case structure is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
inner if within the else portion of the outer if
·         Question 39
2 out of 2 points

When creating a decision table, you must determine how many possible Boolean value combinations exist for the conditions. If there are two conditions, ____ combinations will exist.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
4
·         Question 40
2 out of 2 points

The first step in a loop is typically ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
initialize the variable
·         Question 41
2 out of 2 points

The last step in a loop is usually ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
increment the variable
·         Question 42
2 out of 2 points

The piece of pseudocode that represents incrementing the loop control variable is ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
rep = rep + 1
·         Question 43
2 out of 2 points

When a value is incremented ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
1 is added to the value
·         Question 44
2 out of 2 points

A loop within another loop is known as a(n) ____ loop.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
nested
·         Question 45
2 out of 2 points

When one loop appears inside another, the loop that contains the other loop is called the ____ loop.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
outer
·         Question 46
2 out of 2 points

When a loop control variable is not altered during loop execution, a(n) ____ loop may result.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
infinite
·         Question 47
2 out of 2 points

The ____ loop provides three actions in one compact statement.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
for
·         Question 48
2 out of 2 points

In a for loop, a(n) ____ value is used to control how the loop control variable is incremented.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
step
·         Question 49
2 out of 2 points

Sometimes a ____ loop is called a repeat until loop.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
do until
·         Question 50
2 out of 2 points

The statement that is true of a structured loop is that ____.
Answer
Selected Answer:
Description: https://blackboard.stlcc.edu/images/spacer.gif  
the loop condition represents the only exit from the loop