Fiveable
Fiveable

int

Definition

The int is a primitive data type in Java used to store whole numbers without decimal points. It has a fixed size and represents integer values within a specific range.

Analogy

Think of an int variable as a parking space with limited capacity - it can only hold one car at a time. The size of the parking space determines how many cars (integer values) it can accommodate.

Related terms

double: A primitive data type representing real numbers with decimal points.

arithmetic operators: Mathematical symbols (+, -, *, /) used to perform calculations on numeric data types.

casting: The process of converting one data type to another, such as converting an int to a double.

"int" appears in:

Practice Questions (20+)

  • Evaluate the expression 'int y = 10 % 4'.
  • Evaluate the expression 'int c = 15 % 6'.
  • What is the value of b after the following code is executed? int b = 8; b--; b += 2;
  • What is the value of `i` in `int i = (int)(1.3 + .5);`?
  • What is the value of _e_ in _int e = (int) 3.7 + (int) 2.4;_?
  • Consider the following method: public void printFibonacci(int n) { int a = 0; int b = 1; System.out.print(a + " " + b + " "); for (int i = 2; i < n; i++) { int sum = a + b; System.out.print(sum + " "); a = b; b = sum; } } Assume that the method printFibonacci(6) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printEvenNumbers(int n) { for (int i = 2; i <= n; i += 2) { System.out.print(i + " "); } } Assume that the method printEvenNumbers(8) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printPowersOfTwo(int n) { for (int i = 0; i <= n; i++) { int power = (int) Math.pow(2, i); System.out.print(power + " "); } } Assume that the method printPowersOfTwo(5) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printDigits(int n) { while (n != 0) { int digit = n % 10; System.out.print(digit + " "); n /= 10; } } Assume that the method printDigits(12345) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printFactors(int n) { for (int i = 1; i <= n; i++) { if (n % i == 0) { System.out.print(i + " "); } } } Assume that the method printFactors(24) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void printPrimeNumbers(int n) { for (int i = 2; i <= n; i++) { boolean isPrime = true; for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0) { isPrime = false; break; } } if (isPrime) { System.out.print(i + " "); } } } Assume that the method printPrimeNumbers(20) appears in a method in the same class. What is printed as a result of the method call?
  • Consider the following method: public void calculatePower(double base, int exponent) { double result = Math.pow(base, exponent); printPower(base, exponent, result); } public void printPower(double base, int exponent, double result) { System.out.print(base + "^" + exponent + " = " + result); } Assume that the method calculatePower(2, 3) appears in a method in the same class. What is printed as a result of the method call?
  • What is the output of the following code? public class Calculator { public int add(int x, int y) { return x + y; } public int multiply(int x, int y) { return x * y; } public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(calc.add(5, 6) + calc.multiply(2, 3)); } }
  • What will the following code print out? public class NamePrinter { public void printName(String name) { System.out.println("Your name is: " + name); } public void printAge(int age) { System.out.println("Your age is: " + age); } public static void main(String[] args) { NamePrinter np = new NamePrinter(); np.printName("John"); np.printAge(32); } }
  • What will the following code output? public class Book { public String title; public String author; public int pages; public Book(String bookTitle, String bookAuthor, int bookPages) { title = bookTitle; author = bookAuthor; pages = bookPages; } public void printBookInfo() { System.out.println("Title: " + title); System.out.println("Author: " + author); System.out.println("Pages: " + pages); } public static void main(String[] args) { Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", 180); book.printBookInfo(); } }
  • What is the output of the following code? public class Employee { public String name; public int salary; public Employee(String empName, int empSalary) { name = empName; salary = empSalary; } public void printEmployeeInfo() { System.out.println("Name: " + name); System.out.println("Salary: $" + salary); } public void giveRaise(int raiseAmount) { salary += raiseAmount; } public static void main(String[] args) { Employee emp = new Employee("John", 50000); emp.printEmployeeInfo(); emp.giveRaise(2000); emp.printEmployeeInfo(); } }
  • Consider the following class definition. public class Animal { private String species; private int age; private double weight; public Animal(String animalSpecies, int animalAge, double animalWeight) { species = animalSpecies; age = animalAge; weight = animalWeight; } public void eat() { weight += 5; } public double getWeight() { return weight; } } Assume that the following code segment appears in a class other than Animal. java Copy code Animal dog = new Animal("Dog", 3, 15); dog.eat(); System.out.println(dog.getWeight()); What is printed as a result of executing the code segment?
  • Consider the following class definition: public class Student { private String name; private int age; private String major; public Student(String studentName, int studentAge, String studentMajor) { name = studentName; age = studentAge; major = studentMajor; } public String getName() { return name; } public int getAge() { return age; } public String getMajor() { return major; } } Assume that the following code segment appears in a class other than Student: Student student = new Student("Alice", 19, "Computer Science"); String name = student.getName(); int age = student.getAge(); String major = student.getMajor(); System.out.println(name + " is " + age + " years old and majoring in " + major + "."); What is printed as a result of executing the code segment?
  • Consider the following method: public static int powerOfTwo(int n) { if (n == 0) { return 1; } else { return 2 * powerOfTwo(n - 1); } } Assume that the following code segment appears in a class: int result = powerOfTwo(3); System.out.println("2 raised to the power of 3: " + result); What is printed as a result of executing the code segment?
  • Consider the following method: public static boolean isEven(int number) { return number % 2 == 0; } Assume that the following code segment appears in a class: boolean result = isEven(7); System.out.println("Is 7 an even number? " + result); What is printed as a result of executing the code segment?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.